我正在开发一个包含许多运行超时和间隔的ajax web应用程序.现在我需要清除所有正在运行的超时和间隔.是否有一种简单的方法来阻止所有内容而无需存储每个超时和间隔ID并迭代它们并清除它们?
Sea*_*ney 75
有时可以保存计时器Id/Handle以便稍后清除它,这将是最佳解决方案.所以这是第二好的.但我想更好地了解正在发生的事情.它基本上抓住了最高的计时器ID并清除了所有内容.但是也可以清除其他你不想清除的计时器!
这有点hackish,所以要警告!
// Set a fake timeout to get the highest timeout id
var highestTimeoutId = setTimeout(";");
for (var i = 0 ; i < highestTimeoutId ; i++) {
clearTimeout(i);
}
Run Code Online (Sandbox Code Playgroud)
mpl*_*jan 26
阅读完副本后更新的答案我用 - 关闭了这个问题 -
它在OSX上的Chrome中运行和测试
// run something
var id1 = setInterval(function() { console.log("interval", new Date())}, 1000);
var id2 = setTimeout(function() { console.log("timeout 1", new Date())}, 2000);
var id3 = setTimeout(function() { console.log("timeout 2", new Date())}, 5000); // not run
setTimeout(function() { console.log("timeout 3", new Date())}, 6000); // not run
// this will kill all intervals and timeouts too in 3 seconds.
// Change 3000 to anything larger than 10
var killId = setTimeout(function() {
for (var i = killId; i > 0; i--) clearInterval(i)
}, 3000);
console.log(id1, id2, id3, killId); // the IDs set by the function I used
Run Code Online (Sandbox Code Playgroud)
注意:查看具有数字类型的窗口对象 - 有趣的是,IE分配了一个8位数字,FF是一个以2开头的单个数字
Gok*_*urk 16
这是一个解决方法.
window.timeoutList = new Array();
window.intervalList = new Array();
window.oldSetTimeout = window.setTimeout;
window.oldSetInterval = window.setInterval;
window.oldClearTimeout = window.clearTimeout;
window.oldClearInterval = window.clearInterval;
window.setTimeout = function(code, delay) {
var retval = window.oldSetTimeout(code, delay);
window.timeoutList.push(retval);
return retval;
};
window.clearTimeout = function(id) {
var ind = window.timeoutList.indexOf(id);
if(ind >= 0) {
window.timeoutList.splice(ind, 1);
}
var retval = window.oldClearTimeout(id);
return retval;
};
window.setInterval = function(code, delay) {
var retval = window.oldSetInterval(code, delay);
window.intervalList.push(retval);
return retval;
};
window.clearInterval = function(id) {
var ind = window.intervalList.indexOf(id);
if(ind >= 0) {
window.intervalList.splice(ind, 1);
}
var retval = window.oldClearInterval(id);
return retval;
};
window.clearAllTimeouts = function() {
for(var i in window.timeoutList) {
window.oldClearTimeout(window.timeoutList[i]);
}
window.timeoutList = new Array();
};
window.clearAllIntervals = function() {
for(var i in window.intervalList) {
window.oldClearInterval(window.intervalList[i]);
}
window.intervalList = new Array();
};
Run Code Online (Sandbox Code Playgroud)
它适用于执行这些行后调用的set/clear超时/间隔函数.试着看看它是否有效:
setInterval('console.log(\'a\')', 1000);
setInterval('console.log(\'b\')', 500);
setInterval('console.log(\'c\')', 750);
setTimeout('clearAllIntervals()', 10000);
Run Code Online (Sandbox Code Playgroud)
代理会带来魔力.
小智 13
var noofTimeOuts = setTimeout('');
for (var i = 0 ; i < noofTimeOuts ; i++) clearTimeout(i);
Run Code Online (Sandbox Code Playgroud)
var max = setTimeout(function(){ /* Empty function */ },1);
for (var i = 1; i <= max ; i++) {
window.clearInterval(i);
window.clearTimeout(i);
if(window.mozCancelAnimationFrame)window.mozCancelAnimationFrame(i); // Firefox
}
Run Code Online (Sandbox Code Playgroud)
没有任何内置功能,但是通过调用此clearAll()
函数可以很容易地遍历所有当前未完成的延迟执行函数:
function clearAll() {
for (var i = setTimeout(function() {}, 0); i > 0; i--) {
window.clearInterval(i);
window.clearTimeout(i);
if (window.cancelAnimationFrame) window.cancelAnimationFrame(i);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您负责运行的页面,并且可以将本机延迟执行函数包装在包装器中,这些包装器当然可以为每个设置器函数配备相应的.clearAll()
:
(function(deferFunctions) {
for (var setter in deferFunctions) (function(setter, clearer) {
var ids = [];
var startFn = window[setter];
var clearFn = window[clearer];
function clear(id) {
var index = ids.indexOf(id);
if (index !== -1) ids.splice(index, 1);
return clearFn.apply(window, arguments);
}
function set() {
var id = startFn.apply(window, arguments);
ids.push(id);
return id;
}
set.clearAll = function() { ids.slice(0).forEach(clear); };
if (startFn && clearFn) {
window[setter] = set;
window[clearer] = clear;
}
})(setter, deferFunctions[setter]);
})(
{ setTimeout: 'clearTimeout'
, setInterval: 'clearInterval'
, requestAnimationFrame: 'cancelAnimationFrame'
});
Run Code Online (Sandbox Code Playgroud)
要尝试它的工作原理,您可以尝试执行此操作,例如,这将保持沉默,因为在再次取消之前没有任何回调最终会触发:
// Some example timers of all types:
requestAnimationFrame(console.error);
setInterval(console.info, 1000, 'interval');
setTimeout(alert, 0, 'timeout');
// Now you can clear all deferred functions
// by execution type, whenever you want to:
window.setTimeout.clearAll();
window.setInterval.clearAll();
window.requestAnimationFrame.clearAll();
Run Code Online (Sandbox Code Playgroud)