-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to clear all timeouts and intervals? #174
Comments
Not as far as I'm aware. I'd probably wrap |
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:
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
"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.
Following loop will clear all intervals
|
In the browser context
window.setTimeout
andwindow.setInterval
return an integer, so users can clean up like this (test this code in the browser console or source snippets):In Node.js timers return an object with no exposed properties or methods, except
ref
andunref
. Is there still a way to clear all timers and intervals in Node.js?The text was updated successfully, but these errors were encountered: