Javascript 如何清除所有间隔?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8635502/
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
How do I clear all intervals?
提问by Nikolay Dyankov
I am using
我在用
varName = setInterval(function() { ... }, 1000);
to set a couple of intervals in a jquery plugin that I'm writing, but when the plugin is reloaded I need to clear those intervals. I tried storing them in variables, like this:
在我正在编写的 jquery 插件中设置几个间隔,但是当插件重新加载时,我需要清除这些间隔。我尝试将它们存储在变量中,如下所示:
(function($){
$.mosaicSlider = function(el) {
var base = this;
var transitionInterval, mainInterval;
...
base.init = function() {
mainInterval = setInverval(function() { ... }, 1000);
}
base.grid = function() {
this.transition() = function() {
transitionInterval = setInterval(function(...) {
}
}
base.init();
And I tried killing those intervals in the base.init() function, like this:
我尝试在 base.init() 函数中杀死这些间隔,如下所示:
clearInterval(transitionInterval);
clearInterval(mainInterval);
And like this:
像这样:
window.oldSetInterval = window.setInterval;
window.setInterval = new function(func, interval) { }
回答by Zirak
Store 'em in an object. Since you're the only one making these intervals, and you know what they are, you can store them and later mess with them as you wish. I'd create an object dedicated for just that, something like:
将它们存储在一个对象中。由于您是唯一创建这些间隔的人,并且您知道它们是什么,因此您可以存储它们,然后根据需要随意处理它们。我会为此专门创建一个对象,例如:
var interval = {
// to keep a reference to all the intervals
intervals : new Set(),
// create another interval
make(callback: (...args: any[]) => void, ms: number, ...args: any[]) {
var newInterval = setInterval(callback, ms, ...args);
this.intervals.add(newInterval);
return newInterval;
},
// clear a single interval
clear(id) {
this.interals.delete(id);
return clearInterval(id);
},
// clear all intervals
clearAll() {
for (var id of this.intervals) {
this.clear(id);
}
}
};
Your first question might be
你的第一个问题可能是
Why make a separate object for just that?
为什么要为此创建一个单独的对象?
Well Watson, it's to keep your hand-made intervals related to your plugin/project away from prying eyes, so you won't mess with other intervals being set in the page not related to your plugin.
好吧,Watson,这是为了让与您的插件/项目相关的手工间隔远离窥探,这样您就不会弄乱页面中设置的与您的插件无关的其他间隔。
Yes, but why can't I store it inside the base object?
是的,但为什么我不能将它存储在基础对象中?
You most certainly can, but I think this way is much cleaner. It separates the logic you do in your base with the weird timeout logic.
你当然可以,但我认为这种方式更干净。它将您在基地中执行的逻辑与奇怪的超时逻辑分开。
Why did you store the intervals inside a Setand not an array?
为什么将间隔存储在Set而不是数组中?
Faster access and a little bit of cleaner code. You can go either way, really.
更快的访问和更简洁的代码。你可以走哪条路,真的。
回答by Sudhir Bastakoti
You could do like,
你可以这样做,
var interval_id = window.setInterval("", 9999); // Get a reference to the last
// interval +1
for (var i = 1; i < interval_id; i++)
window.clearInterval(i);
//for clearing all intervals
回答by Shujaath Khan
Intialize Timer and set it as window object. Window.myIntervalwill hold the ID of the Timer.
初始化 Timer 并将其设置为 window 对象。Window.myInterval将保存计时器的 ID。
like window.myInterval = setInterval(function() { console.log('hi'); }, 1000);
喜欢 window.myInterval = setInterval(function() { console.log('hi'); }, 1000);
To clear Interval you can write like
要清除间隔,您可以这样写
if(window.myInterval != undefined && window.myInterval != 'undefined'){
window.clearInterval(window.myInterval);
alert('Timer cleared with id'+window.myInterval);
}