1. Wrapper signature
debounce takes the function to defer and a delay, returning a new wrapped function.
function debounce(fn, delay) {Typorithm · medium
Step 1 of 8 · intro card
Utility Functions
debounce collapses a burst of calls into one, firing fn only after activity stops for delay milliseconds. Each call clears the pending timer and starts a new one.
debounce returns a wrapper that delays invoking fn until callers stop firing it. It is ideal for search-as-you-type inputs and window resize handlers.
On every call it runs clearTimeout to cancel any pending invocation, then schedules a new one with setTimeout — so only the final call in a burst actually runs fn.
The timer id is held in a closure variable, letting consecutive calls of the returned function share and reset the same pending timer.
Fires fn once after a quiet period.
Each call resets the pending timer.
Great for search input and resize.
function debounce(fn, delay) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}Connect each part of the code to the concept before you type.
debounce takes the function to defer and a delay, returning a new wrapped function.
function debounce(fn, delay) {The timer id lives in the closure so it survives across calls of the returned function.
let timer;Each call cancels the previous pending timer, restarting the quiet period.
clearTimeout(timer);A fresh timer fires fn only after delay passes with no new calls.
timer = setTimeout(() => fn(...args), delay);Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.