Javascript:可以在 settimeout 之前调用 cleartimeout 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6442183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:41:13  来源:igfitidea点击:

Javascript: Is it ok to call cleartimeout before settimeout?

javascripttimer

提问by Kenny

I have a function that sets a timer, and calls itself back upon expiration of the timer.

我有一个设置计时器的函数,并在计时器到期时调用自己。

What I'd like to know is if it is bad practice to clear the timer at the top of the function.

我想知道的是清除函数顶部的计时器是否是不好的做法。

The reason for this is because I will be calling that function asynchronously from time to time, and if I don't clear the timer first, I'll have two running simultaneously.

这样做的原因是因为我会不时地异步调用该函数,如果我不先清除计时器,我将有两个同时运行。

I realize that I can clear the timer right before I make the other call to the function, but I'm wondering if it will cause problems in any browser if I just keep the cleartimeout call inside the function which contains the timer.

我意识到我可以在对函数进行另一个调用之前清除计时器,但是我想知道如果我只是将 cleartimeout 调用保留在包含计时器的函数中,它是否会在任何浏览器中引起问题。

One other thought - Can I test the timer variable before making the cleartimeout call, to see if it is a timer?

另一个想法 - 我可以在调用 cleartimeout 之前测试 timer 变量,看看它是否是一个计时器吗?

Here is some example code:

下面是一些示例代码:

function onAir(){

    // reset timer  
    clearTimeout(timer);

    $.getJSON("http://mywebsite.com?format=json&callback=?",function(data){
        if(data.result == '1'){
            do stuff here   
        }
        else{
            do other stuff here
        }   
    });

    // start timer
    timer = setTimeout("onAir()",60000);
} 

Thanks for sharing your brain with me!

感谢您与我分享您的大脑!

Kenny

肯尼

采纳答案by Pointy

Yes, that's fine. Also, you should call "setTimeout()" like this:

是的,没关系。此外,您应该像这样调用“setTimeout()”:

  timer = setTimeout(onAir, 60000);

回答by Naftali aka Neal

Yes you can call clearTimeout on a nullvariable.

是的,您可以对null变量调用 clearTimeout 。

Also i would suggest you change your setTimeoutso it won't use eval:

另外我建议你改变你的setTimeout所以它不会使用eval

timer = setTimeout(onAir,60000);

回答by zzzzBov

Yes you can call clearTimeout(timer), but there are some edge cases where it may cause issues.

是的,您可以调用clearTimeout(timer),但在某些边缘情况下可能会导致问题。

If timerhad been previously set with some other integer value, you might be killing off a completely unrelated timer.

如果timer之前已经设置了一些其他整数值,您可能会终止一个完全不相关的计时器。

setTimeoutjust returns an integer index for the timer. If you're not sure if a timer has been previously set, you could add a check before calling clearTimeout:

setTimeout只返回计时器的整数索引。如果您不确定之前是否设置了计时器,您可以在调用之前添加一个检查clearTimeout

if (window.timer)
{
  clearTimeout(timer);
}
...
timer = setTimeout(onAir, duration);

A solution to the possible pollution of the timer variable is to use a closure:

解决定时器变量可能污染的方法是使用闭包:

(function(){
  var timer,
    duration;
  duration = 60000;
  window.onAir = function onAir(){
    ...code...
    if (timer){
      clearTimeout(timer);
    }
    timer = setTimeout(onAir,duration);
  };
});

回答by Robert

Yes, you can call a clearTimeout on a null variable and the world won't implode.

是的,您可以对空变量调用 clearTimeout 并且世界不会内爆。

回答by JMax

Clearing a Timeout raises not problem to me (but i am not a javascript guru).

清除超时对我来说不是问题(但我不是 javascript 专家)。

Btw, you can find intersting things (checking an existing Timeout) on this thread: Check if a timeout has been cleared?

顺便说一句,您可以在此线程上找到有趣的事情(检查现有超时):检查超时是否已清除?