Javascript clearTimeout 如果存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7176765/
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-08-24 01:11:51  来源:igfitidea点击:

clearTimeout if exists

javascript

提问by Gerben

timer_gearexist only in case if I press some button (until 5 sec). But there is another function it can be called any time. In this function I clear the timer and restart it. But first I have to check if the object exists otherwise I get this error: Uncaught ReferenceError: timer_gear is not defined

timer_gear仅当我按下某个按钮(直到 5 秒)时才存在。但是还有另一个函数可以随时调用。在这个函数中,我清除计时器并重新启动它。但首先我必须检查对象是否存在,否则我会收到此错误:Uncaught ReferenceError: timer_gear is not defined

Could you help me to solve this? These does not work.

你能帮我解决这个问题吗?这些都行不通。

if(timer_gear!="undefined")clearTimeout(timer_gear);

if(timer_gear)clearTimeout(timer_gear);

EDIT1: first I misspelled my question: if(!timer => if(timer EDIT2:

EDIT1:首先我拼错了我的问题: if(!timer => if(timer EDIT2:

the full code is:

完整代码是:

function hide_gear(){
    $('#gear_div').animate({opacity: 0}, 1000);
    delete timer_gear; //EDIT3: destroy object
}

...

...

/*gear*/
$('#gear').click(function(){
    $('#gear_div').animate({
        opacity: 1,
      }, 1000, function() {
        timer_gear = setTimeout("hide_gear();",5000);
      });
});
$('#gear').mousemove(function(){
    if( ? ? ? )
    {
        clearTimeout(timer_gear);
        timer_gear = setTimeout("hide_gear();",5000);
    }

});

Results:

结果:

timer_gear// Uncaught ReferenceError timer_gear is not defined
timer_gear != undefined // Uncaught ReferenceError: timer_gear is not defined
typeof timer_gear !== "undefined" // WORKS
typeof timer_gear != "undefined" // WORKS, just tired it
var timer_gear; //at the begining - WORKS, but I did not wanted a new variable if its not necessary

thank you for answers!

谢谢你的回答!

采纳答案by Rocket Hazmat

The 1st one should be:

第一个应该是:

if(typeof timer_gear !== "undefined"){
  clearTimeout(timer_gear);
}

And the 2nd, but this won't workif timer_gearis not defined, so you should use the typeofone above:

第二个,但如果没有定义,这将不起作用timer_gear所以你应该使用typeof上面的一个

if(timer_gear){
  clearTimeout(timer_gear);
}

回答by Gerben

All you need to do is declare timer_gear. clearTimeoutis not the problem here. To quote the MDN; Passing an invalid ID to clearTimeoutdoes not have any effect (and doesn't throw an exception). So just add the following to the top of your code:

您需要做的就是声明timer_gearclearTimeout不是这里的问题。引用MDN;将无效 ID 传递给clearTimeout没有任何效果(并且不会引发异常)。因此,只需将以下内容添加到代码的顶部:

var timer_gear;

No need for all the if's that everyone else is suggesting.

不需要其他人建议的所有 if。

回答by Alex Turpin

If you only want to clear the timer held in the variable timer_gearif it exists, you can do

如果您只想清除保存在变量中的计时器(timer_gear如果存在),您可以这样做

if (timer_gear) clearTimeout(timer_gear);

回答by Xion

You need a different condition on your if:

你需要一个不同的条件if

if (typeof(timer_gear) !== "undefined")

or simply:

或者干脆:

if (timer_gear)

回答by David Laberge

Normally that should work:

通常这应该工作:

timer_gear = false;
if(timer_gear != false){
    clearTimeout(timer_gear);
}

回答by Nivas

if(timer_gear)clearTimeout(timer_gear);

or

或者

if(timer_gear != undefined)clearTimeout(timer_gear);