Javascript 如何禁用所有 setTimeout 事件?

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

How can I disable all setTimeout events?

javascriptasynchronouspostbacksettimeout

提问by MonsterMMORPG

I am using ajax and asp.net. iI have a javascript function which creates many other javascript functions with setTimeout. After asynchronous postback happenes, I want to disable all of these setTimeouted events. How can I do that?

我正在使用 ajax 和 asp.net。我有一个 javascript 函数,它使用 setTimeout 创建许多其他 javascript 函数。发生异步回发后,我想禁用所有这些 setTimeouted 事件。我怎样才能做到这一点?

回答by SeanDowney

If you can't get access to the code where the timer is set Nick's answer may not work, so all that I can think of is this hack.

如果您无法访问设置计时器的代码,Nick 的回答可能不起作用,所以我能想到的就是这个 hack。

It is a hack, use with caution!

这是一个黑客,谨慎使用!

// Set a fake timeout to get the highest timeout id
var highestTimeoutId = setTimeout(";");
for (var i = 0 ; i < highestTimeoutId ; i++) {
    clearTimeout(i); 
}

Basically it grabs the highest timer id and clears everything less than that. But it's also possible to clear other timers that you do not want to clear!

基本上它会获取最高的计时器 id 并清除低于此值的所有内容。但是也可以清除您不想清除的其他计时器!

回答by Nick Craver

When you call setTimeout(), store the timer ID so you can clear it. If you're creating many timeouts, then an array is a good option for storing the IDs. For example:

当您调用 时setTimeout(),请存储计时器 ID,以便您可以清除它。如果您要创建许多超时,那么数组是存储 ID 的好选择。例如:

var timeouts = [];
//then, store when you create them
timeouts.push( setTimeout( { ... }, 1000) );

Then when you want to clear them:

然后当你想清除它们时:

for (var i = 0; i < timeouts.length; i++) {
    clearTimeout(timeouts[i]);
}
//quick reset of the timer array you just cleared
timeouts = [];

As @Robertnoted below, clearTimeout()won't throw an error if the timeout has already occurred, so there are no race/timing issues here.

正如@Robert在下面指出的那样,clearTimeout()如果超时已经发生,则不会抛出错误,因此这里没有比赛/时间问题。

回答by Robin

Not sure if you can do this globally, but the most common method is to use clearTimeout. You pass the return value of setTimeout() to clearTimeout(), you could use a global var to store all timeout vars.

不确定您是否可以全局执行此操作,但最常用的方法是使用clearTimeout。您将 setTimeout() 的返回值传递给 clearTimeout(),您可以使用全局变量来存储所有超时变量。

回答by Vaidotas Strazdas

Firstly, I was using this code:

首先,我使用了这个代码:

var x = setTimeout('');
for (var i = 0; i < x; i++)
    clearTimeout(x);

However, this peace of code did not work on Google Chrome. So I made improvement for this:

然而,这种代码的和平在谷歌浏览器上不起作用。所以我为此做了改进:

var x = setTimeout('alert("x");',100000); //It is very low probability that after 100 seconds x timeout will not be cleared
for (var i = 0; i <= x; i++)
    clearTimeout(i);

Finally, it is a hack, as it was mentioned in the comment above, so use it carefully.

最后,它是一个 hack,正如上面的评论中提到的那样,所以请谨慎使用它。

Edit: fixed wrong variable used in loop (use i instead of x)

编辑:修复了循环中使用的错误变量(使用 i 而不是 x)