我能否查看计时器是否仍在运行?

91

我有一个简单的问题,但似乎找不到答案:一旦设置了setTimeout,是否有任何方法可以查看它是否仍然被设置?

if (!Timer)
{
    Timer = setTimeout(DoThis,60000);
}

据我所知,当你使用clearTimeout时,变量仍保持其最后一个值。我刚刚查看的console.log显示Timer为“12”,无论超时是否设置或清除。我是否还需要将变量置为空,或者使用其他变量作为布尔变量,表示是的,我已经设置了此定时器?肯定有一种方法只是检查超时是否仍在运行……对吧?我不需要知道剩余时间,只需要知道它是否仍在运行。

6个回答

76

我所做的是:

var timer = null;

if (timer != null) {
  window.clearTimeout(timer); 
  timer = null;
}
else {
  timer = window.setTimeout(yourFunction, 0);
}

3
使用 setTimeout(yourFunction, 0)setTimeout('yourFunction()', 0) 更安全。 - cprcrack
1
即使在异步世界中,JavaScript 仍然是单线程的。 - matpop
1
我只是想说,如果有一种方法可以询问计时器是否仍在运行,那将是很好的。当时我有一个充满异步请求的页面,发现这个解决方案有点过于简化了,但是我记不清楚足够的细节来进一步讨论,真希望我能记得更多。 - Epirocks
为什么要同时清除和置空?clearTimeout不保证它不会占用内存吗?谢谢。 (抱歉,我知道这是很久以前的事了) - 1252748
3
即使调用了window.clearTimeout,变量timer仍将保留其值(一个计时器ID),因此有必要重置它。 - Benny Code

59

除了开始或停止计时器之外,没有其他与计时器交互的方式。在超时处理程序中,我通常将计时器变量置空,而不是使用标志来指示计时器未运行。在 W3Schools 上有一个很好的关于计时器工作原理的描述。在他们的示例中,使用了一个标志变量。

您看到的值是当前计时器的一个句柄,用于在需要时清除(停止)它。


9
Patrick,那不是一个世俗的原则。有时他们拥有有用的资源。 - user993683
5
OT: 问题在于区分这些时间与其他时间 :) 当然,如果不了解主题,则无法做到。 - superjos
1
我通常在超时中将计时器变量设置为null。 - Faiz Mohamed Haneef
也许你的意思是“在启动计时器之前,我通常将计时器变量设置为null”? - Ian Y.

29

无需检查现有的计时器,只需要在启动计时器之前执行clearTimeout即可。

var timer;
//..
var startTimer = function() {
  clearTimeout(timer);
  timer = setTimeout(DoThis, 6000);
}

在启动新实例之前,这将清除任何计时器。


运行良好,正是我所寻找的。谢谢。这个回答值得更多的赞。 - Stéphane Laurent
9
需要进行检查。仅因为您没有想到需要并不意味着不存在 :) - vsync
在我的情况下,我有一个需要始终运行的setTimeout函数。这个检查是为了确保如果服务器崩溃并重新启动,依赖于setTimeout函数的用户可以在重新启动后立即恢复功能。clearTimeout对于我提出这个问题没有帮助。 - Justin Bleile

5

在计时器中设置另一个变量 Timer_Started = true。当计时器函数调用时,还需要将变量更改为 false

// set 'Timer_Started' when you setTimeout
var Timer_Started = true;
var Timer = setTimeout(DoThis,60000);

function DoThis(){

   // function DoThis actions 
   //note that timer is done.
   Timer_Started = false;

}

function Check_If_My_Timer_Is_Done(){

   if(Timer_Started){
      alert("The timer must still be running.");
   }else{
      alert("The timer is DONE.");
   }

}

3

我通常会将计时器归零:

var alarm = setTimeout(wakeUpOneHourLater, 3600000);
function wakeUpOneHourLater() {
    alarm = null;    //stop alarm after sleeping for exactly one hour
}
//...
if (!alarm) {
    console.log('Oops, waked up too early...Zzz...');
}
else {
    console.log('Slept for at least one hour!');
}

3

我知道这是一个旧帖,但我认为仍有人在寻找这个。

这是我使用的方法:

  1. t 用于下一个目标的Date对象中的毫秒数
  2. timerSys 用于实际间隔
  3. seconds 毫秒阈值已设置

接下来我有一个名为 timer 的函数,其中有1个变量。该函数检查变量是否为 ,如果是,则检查计时器是否已经运行,如果是,则填充 全局变量,如果不是 ,则清除间隔并将 global var timerSys 设置为 false

var t, timerSys, seconds;

function timer(s) {
  if (s && typeof s === "number") {
    if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
      timerSys = setInterval(function() {
        sys();
      }, s);
      t = new Date().setMilliseconds(s);
      seconds = s;
    }
  } else {
    clearInterval(timerSys);
    timerSys = false;
  }
  return ((!timerSys) ? "0" : t)
}

function sys() {
  t = new Date().setMilliseconds(seconds);

}

例子 I

现在,您可以向 sys 函数 添加一行:

function sys() {
  t = new Date().setMilliseconds(seconds);
  console.log("Next execution: " + new Date(t));
//this is also the place where you put functions & code needed to happen when interval is triggerd

}

并执行:

  timer(5000);

控制台每5秒钟输出:

  //output:: Next execution: Sun May 08 2016 11:01:05 GMT+0200 (Romance (zomertijd))

例子二

function sys() {
  t = new Date().setMilliseconds(seconds);
  console.log("Next execution: " + seconds/1000 + " seconds");

}

$(function() {
  timer(5000);
});

每隔5秒在控制台中打印:

      //output:: Next execution: 5 seconds

例子三

var t, timerSys, seconds;

function timer(s) {
  if (s && typeof s === "number") {
    if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
      timerSys = setInterval(function() {
        sys();
      }, s);
      t = new Date().setMilliseconds(s);
      seconds = s;
    }
  } else {
    clearInterval(timerSys);
    timerSys = false;
  }
  return ((!timerSys) ? "0" : t)
}

function sys() {
  t = new Date().setMilliseconds(seconds);
  console.log("Next execution: " + seconds / 1000 + " seconds");

}

$(function() {
  timer(5000);

  $("button").on("click", function() {
    $("span").text(t - new Date());
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Freebeer</button>
<span></span>

请注意,这种方法可以使数值低于0。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接