如何清除所有间隔?

发布于 2024-12-23 08:58:07 字数 804 浏览 0 评论 0原文

我用来

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉城已无爱 2024-12-30 08:58:07

您可以通过创建一个“不执行任何操作”的新计时器(通过为其运行一个空函数,计划在未来数十年运行)来找到“最高”计时器标识符,然后清除 ids 1 和该标识符之间的每个计时器,像这样:

// Get a reference to the last interval + 1
const interval_id = window.setInterval(function(){}, Number.MAX_SAFE_INTEGER);

// Clear any timeout/interval up to that id
for (let i = 1; i < interval_id; i++) {
  window.clearInterval(i);
}

但是,请注意,这并不能保证有效: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:

// Get a reference to the last interval + 1
const interval_id = window.setInterval(function(){}, Number.MAX_SAFE_INTEGER);

// Clear any timeout/interval up to that id
for (let i = 1; i < interval_id; i++) {
  window.clearInterval(i);
}

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.

西瓜 2024-12-30 08:58:07

将它们存储在一个对象中。由于您是唯一进行这些间隔的人,并且您知道它们是什么,因此您可以存储它们,然后根据需要随意处理它们。我会创建一个专门用于此目的的对象,例如:

var interval = {
    // to keep a reference to all the intervals
    intervals : new Set(),
    
    // create another interval
    make(...args) {
        var newInterval = setInterval(...args);
        this.intervals.add(newInterval);
        return newInterval;
    },

    // clear a single interval
    clear(id) {
        this.intervals.delete(id);
        return clearInterval(id);
    },

    // clear all intervals
    clearAll() {
        for (var id of this.intervals) {
            this.clear(id);
        }
    }
};

您的第一个问题可能是

为什么要为此创建一个单独的对象?

好吧,Watson,这是为了让与您的插件/项目相关的手工制作的间隔远离窥探,这样您就不会弄乱页面中设置的与您的插件无关的其他间隔。

是的,但是为什么我不能将它存储在基础对象中?

你当然可以,但我认为这种方式更干净。它将您在基地中执行的逻辑与奇怪的超时逻辑分开。

为什么要将间隔存储在 Set 中 而不是数组?

更快的访问和一些更干净的代码。真的,你可以选择任何一种方式。

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(...args) {
        var newInterval = setInterval(...args);
        this.intervals.add(newInterval);
        return newInterval;
    },

    // clear a single interval
    clear(id) {
        this.intervals.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.

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 Set and not an array?

Faster access and a little bit of cleaner code. You can go either way, really.

一身软味 2024-12-30 08:58:07

初始化Timer并将其设置为窗口对象。 Window.myInterval 将保存计时器的 ID。

window.myInterval = setInterval(function() { console.log('hi'); }, 1000);

要清除间隔,您可以这样写

if(window.myInterval != undefined && window.myInterval != 'undefined'){
    window.clearInterval(window.myInterval);
    alert('Timer cleared with id'+window.myInterval);
}

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

if(window.myInterval != undefined && window.myInterval != 'undefined'){
    window.clearInterval(window.myInterval);
    alert('Timer cleared with id'+window.myInterval);
}
凑诗 2024-12-30 08:58:07

当你设置一个间隔时,你会得到一个指向它的指针。
要清除所有间隔,您需要存储所有间隔:

// Initially
var arr = [];
arr.push(setInterval(function () {
    console.log(1);
  }, 1000));
arr.push(setInterval(function () {
    console.log(2);
  }, 1000));
arr.push(setInterval(function () {
    console.log(3);
  }, 1000));
  

以下循环将清除所有间隔

// Clear multiple Intervals
  arr.map((a) => {
    console.log(a)
    clearInterval(a);
    arr = [];
  })

When you set an interval, you get a pointer to it.
To clear all intervals, you'll need to store all of them:

// Initially
var arr = [];
arr.push(setInterval(function () {
    console.log(1);
  }, 1000));
arr.push(setInterval(function () {
    console.log(2);
  }, 1000));
arr.push(setInterval(function () {
    console.log(3);
  }, 1000));
  

Following loop will clear all intervals

// Clear multiple Intervals
  arr.map((a) => {
    console.log(a)
    clearInterval(a);
    arr = [];
  })
止于盛夏 2024-12-30 08:58:07

对于 Node.js

如果不跟踪它们,就不可能清除所有间隔。因此,假设您想要执行清除操作以确保服务正常终止,或者在任何其他情况下您想要终止所有运行间隔。

解决方案

创建一个处理系统中所有setIntervalSingleton,并使用它来创建间隔,然后在必要时调用其clearAllIntervals方法,使之无效一次所有这些,无需单独跟踪它们。

单例实现(对 TS 的建议,在 JS 中只需删除类型)

class IntervalManager {
  private static instance: IntervalManager;
  private intervals: Set<NodeJS.Timeout>;

  private constructor() {
    this.intervals = new Set<NodeJS.Timeout>();
  }

  public static getInstance(): IntervalManager {
    if (!IntervalManager.instance) {
      IntervalManager.instance = new IntervalManager();
    }

    return IntervalManager.instance;
  }

  public setInterval(callback: () => void, delay: number): NodeJS.Timeout {
    const id = setInterval(callback, delay);
    this.intervals.add(id);
    return id;
  }

  public clearInterval(id: NodeJS.Timeout): void {
    clearInterval(id);
    this.intervals.delete(id);
  }

  public clearAllIntervals(): void {
    for (const id of this.intervals) {
      clearInterval(id);
    }
    this.intervals.clear();
  }
}

export default IntervalManager;

用法(可以在任何和多个文件中使用)

import IntervalManager from './IntervalManager';

const intervalManager = IntervalManager.getInstance();

// Set an interval
const id = intervalManager.setInterval(() => {
  console.log('Hello, world!');
}, 1000);

// Clear a specific interval
intervalManager.clearInterval(id);

// Set multiple intervals
intervalManager.setInterval(() => {
  console.log('Hello, world!');
}, 1000);

intervalManager.setInterval(() => {
  console.log('Hello again, world!');
}, 2000);

// Clear all intervals
intervalManager.clearAllIntervals();

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 its clearAllIntervals 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)

class IntervalManager {
  private static instance: IntervalManager;
  private intervals: Set<NodeJS.Timeout>;

  private constructor() {
    this.intervals = new Set<NodeJS.Timeout>();
  }

  public static getInstance(): IntervalManager {
    if (!IntervalManager.instance) {
      IntervalManager.instance = new IntervalManager();
    }

    return IntervalManager.instance;
  }

  public setInterval(callback: () => void, delay: number): NodeJS.Timeout {
    const id = setInterval(callback, delay);
    this.intervals.add(id);
    return id;
  }

  public clearInterval(id: NodeJS.Timeout): void {
    clearInterval(id);
    this.intervals.delete(id);
  }

  public clearAllIntervals(): void {
    for (const id of this.intervals) {
      clearInterval(id);
    }
    this.intervals.clear();
  }
}

export default IntervalManager;

Usage (can be used in any and multiple files)

import IntervalManager from './IntervalManager';

const intervalManager = IntervalManager.getInstance();

// Set an interval
const id = intervalManager.setInterval(() => {
  console.log('Hello, world!');
}, 1000);

// Clear a specific interval
intervalManager.clearInterval(id);

// Set multiple intervals
intervalManager.setInterval(() => {
  console.log('Hello, world!');
}, 1000);

intervalManager.setInterval(() => {
  console.log('Hello again, world!');
}, 2000);

// Clear all intervals
intervalManager.clearAllIntervals();
优雅的叶子 2024-12-30 08:58:07

这对我有用:

window.stop();

This worked for me:

window.stop();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文