teTra/jsm/utils.js
2023-07-05 23:02:39 +02:00

35 lines
948 B
JavaScript

class Scheduler {
constructor() {
this.intervalTasks = new Map()
this.timeoutTasks = new Map()
}
setInterval(func, delay, ...args) {
this.intervalTasks.set(func, window.setInterval(func, delay, ...args))
}
clearInterval(func) {
if (this.intervalTasks.has(func))
window.clearInterval(this.intervalTasks.get(func))
this.intervalTasks.delete(func)
}
setTimeout(func, delay, ...args) {
if (!this.timeoutTasks.has(func))
this.timeoutTasks.set(func, window.setTimeout(func, delay, ...args))
}
clearTimeout(func) {
if (this.timeoutTasks.has(func))
window.clearTimeout(this.timeoutTasks.get(func))
this.timeoutTasks.delete(func)
}
resetTimeout(func, delay, ...args) {
this.clearTimeout(func)
this.timeoutTasks.set(func, window.setTimeout(func, delay, ...args))
}
}
export { Scheduler }