help icon indicating copy to clipboard operation
help copied to clipboard

Is there a way to clear all timeouts and intervals?

Open vsemozhetbyt opened this issue 10 years ago • 11 comments

In the browser context window.setTimeout and window.setInterval return an integer, so users can clean up like this (test this code in the browser console or source snippets):

window.setTimeout(() => {console.log('Timeout');}, 500);
window.setInterval(() => {console.log('Interval');}, 1000);

let id = window.setTimeout(() => {}, 0);
console.log(id);
while (id) {
  window.clearTimeout(id);
  id--;
}

id = window.setInterval(() => {}, 0);
console.log(id);
while (id) {
  window.clearInterval(id);
  id--;
}

In Node.js timers return an object with no exposed properties or methods, except ref and unref. Is there still a way to clear all timers and intervals in Node.js?

vsemozhetbyt avatar May 22 '16 21:05 vsemozhetbyt

Not as far as I'm aware. I'd probably wrap setInterval and setTimeout in another function that pushes the ids into an array, to keep track of them.

SomeoneWeird avatar May 24 '16 03:05 SomeoneWeird

Yes, this is a solution for timers under control and track. But what if I need to clean up, say, after some incautious and intricate module?

vsemozhetbyt avatar May 24 '16 08:05 vsemozhetbyt

did you have success with this problem @vsemozhetbyt

Knighton910 avatar Jun 15 '16 21:06 Knighton910

Theoretically no, but I've found a workaround for a particular case. If there is no way to resolve this in general, well, the issue could be closed, I think.

vsemozhetbyt avatar Jun 15 '16 21:06 vsemozhetbyt

Something like this hack works pretty well for browser envs, but since Timers don't expose IDs in Node, I haven't found a way to cancel all timers in Node.

My use case is I'm running a 3rd party script, and for some reason even when that script returned, my process kept running. Using why-is-node-running I saw that the 3rd party script set a few timeouts, which prevented my script from exiting.

I'd rather not use process.exit since my script is consumed by other scripts, which manage their own process. It seems like there are a few workarounds:

  1. Wrap setTimeout to register timeouts in a global registry before I invoke the 3rd party script
  2. Run the 3rd party script in its own child process, which my process can then kill

Neither of these are pretty. It would be fantastic to have a better way to do this.

bcherny avatar Oct 31 '16 21:10 bcherny

If you'd like this, could you open a feature request on https://github.com/nodejs/node? Thanks!

Fishrock123 avatar Oct 31 '16 21:10 Fishrock123

WebserverTest is also something that refuses to shut down...

InputNeuron avatar Nov 02 '18 00:11 InputNeuron

CacheEntriesFactoryTest refuses to shut down due to the CachingPHZReader setting a timeout of 1m

constructor(path: string, timeout: number = 60000)

InputNeuron avatar Nov 02 '18 00:11 InputNeuron

clearAllSetTimeouts

ES6 solution

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-07-27
 * @modified
 *
 * @description clearAllSetTimeouts
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @augments
 * @example
 * @link
 * @solutions
 *
 */

const log = console.log;

class clearAllSetTimeouts {
  constructor(name) {
    this.name = name;
  }
  static ids = [];
  static add(callback, timeout) {
    const id = setTimeout(() => {
      callback();
    }, timeout);
    log(`add id`, id);
    this.ids.push(id);
  }
  static clearAll() {
    const arr = this.ids;
    let len = this.ids.length;
    while (len > 0) {
      const id = arr[len - 1];
      log(`clear id`, id);
      clearTimeout(id);
      len--;
    }
  }
};

// test
clearAllSetTimeouts.add(() => log(`1`), 0)
clearAllSetTimeouts.add(() => log(`2`), 0)
clearAllSetTimeouts.add(() => log(`3`), 0)

clearAllSetTimeouts.clearAll();

image

xgqfrms avatar Jul 27 '20 17:07 xgqfrms

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

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 = [];
  })

ashishsiddhu avatar Jul 30 '20 22:07 ashishsiddhu

var orgSetInterval = setInterval; var ids =[];

// reset setInterval = function (handler, time) { let id = orgSetInterval(func, time); ids.push(id); }

HuynhAnh avatar Jul 30 '24 04:07 HuynhAnh