// Use this method to reduce many method calls to a few. // It's an 'immediate' implementation, so it will call the callbackindex: number once immediately an then again after the given wait. type CallbackFunc = (value: T, index: number) => void; const debounce = (callback: CallbackFunc, wait: number) => { let timer: ReturnType; let lastCall = 0; return (...args: Parameters) => { const [value, index] = args; clearTimeout(timer); const now = Date.now(); const timeFromLastCall = now - lastCall; if (timeFromLastCall > wait) { lastCall = now; callback(value, index); } else { timer = setTimeout(() => { lastCall = now; callback(value, index); }, wait); } }; }; export default debounce;