@figliolia/react-hooks
Version:
A small collection of simple React Hooks you're probably rewriting on a regular basis
25 lines (24 loc) • 515 B
JavaScript
export class Timeout {
IDs = new Set();
execute(callback, delay = 0) {
const ID = setTimeout(() => {
callback();
}, delay);
this.IDs.add(ID);
return () => {
this.clear(ID);
};
}
abortAll() {
for (const ID of this.IDs) {
clearTimeout(ID);
}
this.IDs.clear();
}
clear(ID) {
if (ID && this.IDs.has(ID)) {
clearTimeout(ID);
this.IDs.delete(ID);
}
}
}