← Back to Gists

debounce function implementation

📝 JavaScript
jorgeharrell188
jorgeharrell188 · Level 11 ·

This 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

0

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.