Is there a way to clear all timeouts and intervals?
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?
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.
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?
did you have success with this problem @vsemozhetbyt
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.
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:
- Wrap setTimeout to register timeouts in a global registry before I invoke the 3rd party script
- 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.
If you'd like this, could you open a feature request on https://github.com/nodejs/node? Thanks!
WebserverTest is also something that refuses to shut down...
CacheEntriesFactoryTest refuses to shut down due to the CachingPHZReader setting a timeout of 1m
constructor(path: string, timeout: number = 60000)
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();

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 = [];
})
var orgSetInterval = setInterval; var ids =[];
// reset setInterval = function (handler, time) { let id = orgSetInterval(func, time); ids.push(id); }