import { v4 } from '../uuid/index' /** * 定时任务 */ export const timerTask = new (class { private timeoutMap = new Map(); private intervalMap = new Map(); /** * 设置定时器 * @param callback * @param delay * @param timerId */ setTimeout(callback: () => void, delay = 100, timerId?: string) { const uuid = timerId || v4(); this.clearTimeout(uuid); const timer = setTimeout(() => { this.timeoutMap.delete(uuid); callback(); }, delay) this.timeoutMap.set(uuid, timer); return { uuid, clear: () => this.clearTimeout(uuid) } } /** * 设置周期定时器 * @param callback * @param delay * @param timerId */ setInterval(callback: () => void, delay = 100, timerId?: string) { const uuid = timerId || v4(); this.clearInterval(uuid); const timer = setInterval(() => { callback(); }, delay) this.intervalMap.set(uuid, timer); return { uuid, clear: () => this.clearInterval(uuid) } } /** * 清除定时器 * @param keys */ clearTimeout(...keys: string[]) { if (keys.length) { keys.forEach((key) => { if (this.timeoutMap.has(key)) { clearTimeout(this.timeoutMap.get(key)); this.timeoutMap.delete(key); } }) } else { for (const [key, value] of this.timeoutMap.entries()) { clearTimeout(value); this.timeoutMap.delete(key); } } } /** * 清除周期定时器 * @param keys */ clearInterval(...keys: string[]) { if (keys.length) { keys.forEach((key) => { if (this.intervalMap.has(key)) { clearInterval(this.intervalMap.get(key)); this.intervalMap.delete(key); } }) } else { for (const [key, value] of this.intervalMap.entries()) { clearInterval(value); this.intervalMap.delete(key); } } } /** * 清除所有定时器 */ clearAll() { this.clearTimeout(); this.clearInterval(); } }) /** * 定时拦截器 */ export const timerInterceptor = new (class { private debounceMap = new Map(); private throttleMap = new Map(); /** * 函数防抖(等待触发) * @param callback 回调函数 * @param delay 延迟毫秒数,默认100毫秒 * @returns */ setDebounce(callback: (...params: T) => void, delay = 100) { let timer = 0; return function (...args: T) { clearTimeout(timer); timer = setTimeout(() => { callback(...args); }, delay) } } /** * 函数节流(间隔触发) * @param callback 回调函数 * @param delay 延迟毫秒数,默认100毫秒 * @returns */ setThrottle(callback: (...params: T) => void, delay = 100) { let timer = 0; return function (...args: T) { if (timer === 0) { timer = setTimeout(() => { timer = 0; callback(...args); }, delay) } } } /** * 函数防抖(等待触发) * @param callback 回调函数 * @param delay 延迟毫秒数,默认100毫秒 * @param timerId 计时器ID */ debounce(callback: () => void, delay = 100, timerId = 'timer') { if (this.debounceMap.has(timerId)) { const t = this.debounceMap.get(timerId); clearTimeout(t); } const timer = setTimeout(() => { this.debounceMap.delete(timerId); callback(); }, delay) this.debounceMap.set(timerId, timer); } /** * 函数节流(间隔触发) * @param callback 回调函数 * @param delay 延迟毫秒数,默认100毫秒 * @param timerId 计时器ID * @returns */ throttle(callback: () => void, delay = 100, timerId = 'timer') { if (this.throttleMap.has(timerId)) { const t = this.throttleMap.get(timerId); if (t) return; } let timer = setTimeout(() => { timer = 0; this.throttleMap.delete(timerId); callback(); }, delay) this.throttleMap.set(timerId, timer); } })