如何清除所有间隔?
我用来
varName = setInterval(function() { ... }, 1000);
在我正在编写的 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();
我尝试在 base.init() 函数中删除这些间隔,如下所示:
clearInterval(transitionInterval);
clearInterval(mainInterval);
像这样:
window.oldSetInterval = window.setInterval;
window.setInterval = new function(func, interval) { }
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:
(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:
clearInterval(transitionInterval);
clearInterval(mainInterval);
And like this:
window.oldSetInterval = window.setInterval;
window.setInterval = new function(func, interval) { }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过创建一个“不执行任何操作”的新计时器(通过为其运行一个空函数,计划在未来数十年运行)来找到“最高”计时器标识符,然后清除 ids 1 和该标识符之间的每个计时器,像这样:
但是,请注意,这并不能保证有效:HTML 标准 确实实际上并没有规定如何分配超时/间隔标识符,因此我们将获得对“最后一个间隔标识符 + 1”的引用的假设仅仅是:一个假设。在回答这个问题时,对于大多数浏览器来说这可能是正确的,但不能保证即使在同一浏览器的下一个版本中情况仍然如此。
You can find the "highest" timer identifier by creating a new timer that does "nothing" (by giving it an empty function to run, scheduled to run decades in the future), and then clear every timer between ids 1 and that identifier, like so:
However, note that this is not guaranteed to work: the HTML standard does not actually prescribe how timeout/interval identifiers get allocated, so the assumption that we'll get a reference to the "last interval identifier + 1" is merely that: an assumption. It might be true for most browsers at the time of this answer, but there is no guarantee that's that will still be the case even in the next version of the same browser.
将它们存储在一个对象中。由于您是唯一进行这些间隔的人,并且您知道它们是什么,因此您可以存储它们,然后根据需要随意处理它们。我会创建一个专门用于此目的的对象,例如:
您的第一个问题可能是
好吧,Watson,这是为了让与您的插件/项目相关的手工制作的间隔远离窥探,这样您就不会弄乱页面中设置的与您的插件无关的其他间隔。
你当然可以,但我认为这种方式更干净。它将您在基地中执行的逻辑与奇怪的超时逻辑分开。
更快的访问和一些更干净的代码。真的,你可以选择任何一种方式。
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:
Your first question might be
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.
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.
Faster access and a little bit of cleaner code. You can go either way, really.
初始化Timer并将其设置为窗口对象。 Window.myInterval 将保存计时器的 ID。
像
window.myInterval = setInterval(function() { console.log('hi'); }, 1000);
要清除间隔,您可以这样写
Intialize Timer and set it as window object. Window.myInterval will hold the ID of the Timer.
like
window.myInterval = setInterval(function() { console.log('hi'); }, 1000);
To clear Interval you can write like
当你设置一个间隔时,你会得到一个指向它的指针。
要清除所有间隔,您需要存储所有间隔:
以下循环将清除所有间隔
When you set an interval, you get a pointer to it.
To clear all intervals, you'll need to store all of them:
Following loop will clear all intervals
对于 Node.js
如果不跟踪它们,就不可能清除所有间隔。因此,假设您想要执行清除操作以确保服务正常终止,或者在任何其他情况下您想要终止所有运行间隔。
解决方案
创建一个处理系统中所有
setInterval
的Singleton,并使用它来创建间隔,然后在必要时调用其clearAllIntervals
方法,使之无效一次所有这些,无需单独跟踪它们。单例实现(对 TS 的建议,在 JS 中只需删除类型)
用法(可以在任何和多个文件中使用)
For Node.js
There's no possible hack to clear all intervals without keeping track of them. So let's say you want to perform a clear to make sure the service terminates graciously or in any other scenario you want to kill all running intervals.
Solution
Create a Singleton that handles all
setInterval
in the system and use it to create the intervals, then, when necessary call itsclearAllIntervals
method, invalidating all of them at once without having to keep track of them individually.Singleton Implementation (Suggestion for TS, in JS just remove typings)
Usage (can be used in any and multiple files)
这对我有用:
This worked for me: