CKTIS / JavaScript
Debounce and throttle: controlling function calls
Debounce postpones execution until a burst of calls becomes quiet, while throttle limits execution frequency during a burst. Distinguish leading and trailing variants: they determine whether the first call, the last call, or both are retained.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const debounced = debounce(fn, wait);
const throttled = throttle(fn, wait);
debounced(...args);
throttled(...args);debounce / throttle are call-control patterns, not built-in ECMAScript functions. Trailing debounce runs a function after a quiet period of wait: each new call cancels the previous timer and schedules another. It retains the latest arguments and this. This suits starting a search after a typing pause; continuous calls can postpone execution indefinitely.
Throttle limits execution frequency even while calls keep arriving. The example's leading variant executes the first call immediately, then discards subsequent calls until a timer unblocks it. It does not execute the discarded final call. It can limit how often scroll handling runs. Leading means execution at the start of a window; trailing means deferred execution at its end. Implementations may offer both variants, so define these rules explicitly.
The example uses ECMAScript 2015 syntax and browser setTimeout() and clearTimeout(), which HTML specifies rather than ECMAScript. Timers are available in window and worker contexts; support in Node 24 does not prove browser API support. Browser setTimeout() converts the delay to a signed 32-bit integer. For example, 2147483648 ms wraps to a negative value and is treated as zero. A delay does not guarantee an exact execution time: other tasks can postpone execution, and sufficiently nested timers have a 4 ms minimum. This is not a real-time mechanism.
Create the wrapper once and reuse it; recreating it for every event loses the shared state. The example assumes a valid fn and an integer wait in milliseconds satisfying 0 <= wait && wait <= 2147483647. It does not forward the function's return value and offers no cancel(), flush(), or maxWait. In production, arrange to cancel pending work when removing a component. Canceling a timer neither stops a request already started nor protects against stale responses.
Examples
function debounce(fn, wait) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
timer = undefined;
fn.apply(this, args);
}, wait);
};
}
function throttle(fn, wait) {
let blocked = false;
return function (...args) {
if (blocked) return;
blocked = true;
setTimeout(() => { blocked = false; }, wait);
fn.apply(this, args);
};
}
const debounced = debounce(value => console.log('debounce:', value), 50);
const throttled = throttle(value => console.log('throttle:', value), 50);
for (const value of ['A', 'B', 'C']) {
debounced(value);
throttled(value);
}Result
throttle: A debounce: C
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.