1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function throttle(func: (...args: any[]) => void, delay: number = 100) { let lastTime = 0; return (...args: any[]) => { if (args[0] instanceof Event) { args[0].preventDefault(); } const now = Date.now(); if (now - lastTime >= delay) { func.apply(this, args) lastTime = now } } }
function debounce(func, delay = 1000) { let timer = null; return () => { if (timer) { clearTimeout(timer) } timer = setTimeout(() => { func.apply(this, arguments) timer = null }, delay) } }
|