Skip to content
IT glossary
FrontendUpdated December 3, 2026

Debounce and throttle

Debounce и throttle

Both techniques reduce how often a function runs in response to frequent events. Debounce waits for a quiet period; throttle limits how often the function can run while events keep coming.

Also known as: debounce, throttle

When to use each one

Ask whether the user needs feedback during an action or only after a pause. For search suggestions, intermediate queries are often not worth sending: debounce the request so it runs after the user stops typing. For a scroll progress indicator, waiting until scrolling stops defeats the purpose: throttle the updates so the indicator can move throughout the interaction without updating on every event.

Neither technique stops the browser from emitting events. Instead, you pass those events to a wrapper that decides whether and when to call your function.

What happens over time

A trailing debounce starts a timer and restarts it on every call. Once the calls stop for long enough, the wrapper runs the function with the latest arguments. If the calls never let up, the function may never run. Some libraries offer a maximum wait to prevent that.

A leading-only throttle lets the first call through immediately, then drops calls until the interval has elapsed. The next call after that interval starts a new one. This keeps updates coming during sustained activity, but it can miss the final value. A throttle with a trailing call can deliver the latest suppressed value instead of dropping it.

Suppose calls arrive at 0, 80, 160, and 240 ms, with a 200 ms interval. A trailing debounce runs once, around 440 ms. A leading-only throttle runs at 0 and 240 ms. The debounce time is approximate: JavaScript can't run the timer callback while the main thread is busy.

Example: a leading-only throttle

function throttle(fn, interval) {
  let lastRun = -Infinity;
  return function (...args) {
    const now = performance.now();
    if (now - lastRun < interval) return;
    lastRun = now;
    return fn.apply(this, args);
  };
}

const updateProgress = throttle(renderScrollProgress, 100);
window.addEventListener('scroll', updateProgress);

The wrapper keeps the last execution time in a closure and uses apply to forward the caller's this and arguments. This version has no timer or trailing call. If the user stops scrolling during the blocked interval, the indicator may not show the final position. If that matters, change the behavior rather than just shortening the interval.

Common mistakes and interview questions

Create the wrapper once per event stream, not inside the event handler. Otherwise, each event gets fresh state and the wrapper can't enforce a shared interval. The same issue can appear when a React component recreates a wrapper on every render. On cleanup, remove the listener and cancel any pending timer if the implementation uses one.

Debouncing a search request doesn't cancel an earlier request or guarantee that responses arrive in order. If an older response must not replace newer results, abort obsolete requests or ignore their responses.

In a coding interview, clarify the contract before writing the wrapper. Should a single call run immediately, run after a delay, or do something else? What happens to the final call in a burst? Once the behavior is agreed on, use a short timeline or fake timers to test a single call, repeated calls, and calls at the interval boundary.