Skip to content

Debounce

debounce is a utility function that delays the execution of a callback until a specified amount of time has passed since the last time it was invoked. It’s useful for optimizing performance, especially in events like input changes, window resizing, or scrolling.

export function debounce(callback, wait) {
let timeoutId = null;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}
NameTypeDescription
callbackFunctionThe function to be executed after the debounce time.
waitNumberThe number of milliseconds to delay after the last call.

Function: A debounced version of the original function.

const log = debounce((msg) => {
console.log(msg);
}, 500);
// Simulate rapid calls
log('first call');
log('second call');
log('third call');
// Only "third call" will be logged after 500ms