debounce function implementation
📝 JavaScriptThis debounce function delays executing a callback until after a specified wait period has passed since the last invocation, preventing excessive calls from rapid events.
JavaScript
function debounce(callback, wait) { let timeout; return function(...args) { clearTimeout(timeout); timeout = setTimeout(() => callback.apply(this, args), wait); };
}
Comments
I had to debug a case last week where the trailing call was firing on unmount, so I ended up passing a cancel token through the closure.