如何禁用所有setTimeout的事件? [英] How can I disable all setTimeout events?
问题描述
我使用AJAX和asp.net。第二节还有它与的setTimeout创造了许多其他的JavaScript函数JavaScript函数。异步回发一切发生的时候,我要禁用所有这些setTimeouted事件。我该怎么做?
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?
推荐答案
当你调用的setTimeout()
,存储计时器ID,因此您可以清除它。如果你创造了许多超时,那么一个数组是用于存储ID的一个不错的选择。例如:
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:
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 = [];
正如 @Robert 的下面所指出的, clearTimeout()
不会抛出一个错误,如果已经发生超时,因此在这里没有种族/时序问题。
As @Robert noted below, clearTimeout()
won't throw an error if the timeout has already occurred, so there are no race/timing issues here.
这篇关于如何禁用所有setTimeout的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!