【问题标题】:Can I see if a timer is still running?我可以查看计时器是否仍在运行吗?
【发布时间】:2011-03-15 21:36:30
【问题描述】:

这里有一个我似乎找不到答案的简单问题:一旦设置了setTimeout,有什么方法可以查看它是否仍然设置好?

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

据我所知,当您clearTimeout 时,变量保持在其最后一个值。 console.log 我刚刚看到显示 Timer 为“12”,无论超时是否已设置或清除。我是否也必须清空变量,或者使用其他变量作为布尔值,是的,我已经设置了这个计时器?当然有一种方法可以检查超时是否仍在运行......对吗?我不需要知道还剩多长时间,只要它还在运行即可。

【问题讨论】:

    标签: javascript settimeout


    【解决方案1】:

    我做的是:

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

    【讨论】:

    • 使用setTimeout(yourFunction, 0),比setTimeout('yourFunction()', 0)安全。
    • @Epirocks 即使在异步世界中,Javascript 仍然是单线程的。
    • 我只是在指出,如果有办法询问计时器是否仍在运行,那就太好了。当时我有一个充满异步请求的页面,并且发现上面的解决方案有点过于简化,但记不住详细信息,希望我有。
    • 为什么要清除为空? clearTimeout 不能确保它不占用内存吗?干杯。 (对不起,我知道这是很久以前的事了)
    • @1252748,变量 timer 仍然保持其值(计时器 ID),即使在调用 window.clearTimeout 时也是如此。所以需要重新设置。
    【解决方案2】:

    除了启动或停止它之外,无论如何都无法与计时器交互。我通常将超时处理程序中的计时器变量设为空,而不是使用标志来指示计时器未运行。 W3Schools 上有一个关于计时器如何工作的很好的描述。在他们的示例中,他们使用了一个标志变量。

    您看到的值是当前计时器的handle,在您清除(停止)它时使用。

    【讨论】:

    • -1 patrick,这不是世俗的原则。有时他们有有用的资源。
    • OT:问题在于将这些时间与其他时间区分开来 :) 当然,在不了解主题的情况下
    • "我通常在超时时将计时器变量设为空"
    【解决方案3】:

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

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

    这将在启动新实例之前清除所有计时器。

    【讨论】:

    • 工作正常,完全符合我的要求。谢谢。这个答案值得更多的支持。
    【解决方案4】:

    用你的计时器设置另一个变量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.");
       }
    
    }
    

    【讨论】:

      【解决方案5】:

      我通常会取消计时器:

      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!');
      }
      

      【讨论】:

        【解决方案6】:

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

        这是我使用的: 3个变量:

        1. t 为毫秒以来......在下一个目标的日期对象中
        2. timerSys 为实际间隔
        3. seconds 毫秒阈值已设置

        接下来我有一个带有 1 个变量的 function timer,该函数检查变量是否为 真正,如果是,他检查计时器是否已经在运行,如果是这种情况,则填充 全局vars,如果不是,清除区间并将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);
        
        }
        

        示例一

        现在你可以在 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-04
          相关资源
          最近更新 更多