考试结束后如何停止计时器?

发布于 2024-11-27 23:13:26 字数 760 浏览 0 评论 0原文

我想在考试结束时但在它达到零之前停止这个计时器。请帮助我编写脚本。谢谢。

JavaScript 代码:

var cnt = 165*60; // 165 minutes (2 hours & 45 minutes) convert to seconds

function countdown() {
    if (cnt < 0) {
        document.f.c.value = "- : - - : - -" ;
    }
    else {
        hour = Math.floor(cnt / 3600);
        totalmin = Math.floor(cnt / 60);
        min = totalmin - (hour * 60);
        sec = cnt - (totalmin * 60);
        if (sec < 10) { sec = "0" + sec;}
        if (min < 10) {min = "0" + min;}
        if (hour < 10) {hour = "0" + hour;}
        document.f.c.value = hour + ":" + min + ":" + sec;
        cnt--;
        _timer = setTimeout("countdown()", 1000);
    }
}

var _timer = setTimeout("countdown()", 1000); // tick

I want to stop this timer when I end the exam, but before it reaches zero. Kindly help me out on scripts please. Thanks.

JavaScript code:

var cnt = 165*60; // 165 minutes (2 hours & 45 minutes) convert to seconds

function countdown() {
    if (cnt < 0) {
        document.f.c.value = "- : - - : - -" ;
    }
    else {
        hour = Math.floor(cnt / 3600);
        totalmin = Math.floor(cnt / 60);
        min = totalmin - (hour * 60);
        sec = cnt - (totalmin * 60);
        if (sec < 10) { sec = "0" + sec;}
        if (min < 10) {min = "0" + min;}
        if (hour < 10) {hour = "0" + hour;}
        document.f.c.value = hour + ":" + min + ":" + sec;
        cnt--;
        _timer = setTimeout("countdown()", 1000);
    }
}

var _timer = setTimeout("countdown()", 1000); // tick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三五鸿雁 2024-12-04 23:13:26

我假设您的意思是您希望在倒计时达到 0 之前结束计时器。

首先,您应该使用 setInterval 来代替。它应该适用于所有主要浏览器(包括 IE)。这只是表达“我希望这种情况经常发生”的一种稍微好一点的方式。根据 MDN,它:

重复调用一个函数,每次调用该函数之间有固定的时间延迟。

使用方法如下:

var cnt = 165*60; // 165 minutes (2 hours & 45 minutes) convert to seconds
function countdown() {
  if (cnt < 0) {
   document.f.c.value = "- : - - : - -" ;
  }
  else {
   hour = Math.floor(cnt / 3600);
   totalmin = Math.floor(cnt / 60);
   min = totalmin - (hour * 60);
   sec = cnt - (totalmin * 60);
   if (sec < 10) {sec = "0" + sec;}
   if (min < 10) {min = "0" + min;}
   if (hour < 10) {hour = "0" + hour;}
   document.f.c.value = hour + ":" + min + ":" + sec;
   cnt--;

   if(cnt <= 0) { # Stops the timer when it reaches 0.
       clearInterval(_interval);
   }
  }
}
var _interval = setInterval(countdown, 1000);

页面中的某处有一个可以停止计时器的按钮。

<input type="button" value="Done" onclick="clearInterval(_interval)">

虽然说实话,倒计时器让我感到害怕。我宁愿有一个倒计时。 :-)

I assume you meant that you want to end the timer before the countdown reaches 0.

First of all, you should use setInterval instead. It should work in all major browsers (including IE). It is just a slightly nicer way to express "I want this to happen every so often." According to the MDN, it:

Calls a function repeatedly, with a fixed time delay between each call to that function.

Here's how you would use it:

var cnt = 165*60; // 165 minutes (2 hours & 45 minutes) convert to seconds
function countdown() {
  if (cnt < 0) {
   document.f.c.value = "- : - - : - -" ;
  }
  else {
   hour = Math.floor(cnt / 3600);
   totalmin = Math.floor(cnt / 60);
   min = totalmin - (hour * 60);
   sec = cnt - (totalmin * 60);
   if (sec < 10) {sec = "0" + sec;}
   if (min < 10) {min = "0" + min;}
   if (hour < 10) {hour = "0" + hour;}
   document.f.c.value = hour + ":" + min + ":" + sec;
   cnt--;

   if(cnt <= 0) { # Stops the timer when it reaches 0.
       clearInterval(_interval);
   }
  }
}
var _interval = setInterval(countdown, 1000);

And somewhere in your page have a button that will stop the timer.

<input type="button" value="Done" onclick="clearInterval(_interval)">

Although to be honest, having a countdown timer freaks me out. I'd rather have a count-up timer. :-)

羁〃客ぐ 2024-12-04 23:13:26

在你的其他部分
查看

if(current_time < total_time)
{
   //Set TimeOut
}

In your else part
check

if(current_time < total_time)
{
   //Set TimeOut
}
长途伴 2024-12-04 23:13:26

我认为最好的选择是使用 setInterval 而不是 setTimeout。

setInterval 返回间隔的句柄。 clearInterval(handle) 将取消该间隔。这里有一些伪代码可以帮助您入门:

var global_timer;

function countdown(){
    // do some countdown stuff

    if([we're done]) {
        window.clearInterval(global_timer);
    }
}

global_timer = window.setInterval("countdown()", 1000);

I think your best bet here is to use setInterval rather than setTimeout.

setInterval returns a handle to the interval. clearInterval(handle) will cancel that interval. Here's some pseudo to get you started:

var global_timer;

function countdown(){
    // do some countdown stuff

    if([we're done]) {
        window.clearInterval(global_timer);
    }
}

global_timer = window.setInterval("countdown()", 1000);
浅笑依然 2024-12-04 23:13:26

在用户结束考试时执行的代码中,可能在单击按钮后,只需添加这样的行:

window.clearTimeout(_timer);

计时器将停止计时。

In the code that you execute when user ends the exam, probably after button click, just add such line:

window.clearTimeout(_timer);

And the timer will stop ticking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文