export class TimerManager { constructor() { // 定时器对象数组 this.timers = []; } /** * 添加定时器到数组中 * @param {*} fn * @param {*} delay * @param {...any} args * @returns */ setTimeout(fn, delay, ...args) { const id = window.setTimeout(fn, delay, ...args); this.timers.push(id); return id; } /** * 添加定时器到数组中 * @param {*} fn * @param {*} delay * @param {...any} args * @returns */ setInterval(fn, delay, ...args) { const id = window.setInterval(fn, delay, ...args); this.timers.push(id); return id; } /** * 清除定时器 * @param {*} id */ clearTimeout(id) { window.clearTimeout(id); this.timers = this.timers.filter((timerId) => timerId !== id); } clearInterval(id) { window.clearInterval(id); this.timers = this.timers.filter((timerId) => timerId !== id); } cleanAll() { console.log('cleanAll time or interval'); this.timers.forEach((timerId) => { window.clearTimeout(timerId); window.clearInterval(timerId); }); this.timers = []; } }