Implement debounce() in JavaScript

intermediate
By FrontendAtlas Editorial · Updated Nov 12, 2026
Implement debounce(fn, delay) by returning a trailing-edge wrapper that clears the previous timer and runs fn only after calls stop for delay ms. The wrapper must not call fn synchronously, must run only the latest call in a burst, and must pass that call's this and all of its arguments to fn.

Arguments

  • fn: Function — The function to be executed after the delay.
  • delay: number — The delay in milliseconds before executing the function.

Returns

Function — A debounced version of the original function that delays execution until the delay period has elapsed.

Solution idea

Interview answer: the base prompt is a trailing-edge debounce with one timeoutId; cancel(), flush(), and leading are follow-ups.

Examples
// Example: timeline with delay=100
const log = debounce((value) => console.log(value), 100)
log('A') // t=0
// t=50: log('B') resets the timer
// t=90: log('C') resets it again
// Only C runs at t=190

// Basic example
const sayHello = debounce(() => console.log('Hello!'), 300)
sayHello()

// Multiple rapid calls — only last one runs
const run = debounce(() => console.log('Run!'), 500)
run()
run()
run() // Only this one executes after 500ms

// With arguments
const greet = debounce(name => console.log(`Hi ${name}`), 400)
greet('Alice')
greet('Bob') // Only 'Hi Bob' logs after 400ms

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

Reported in a public Waymo interview account. Waymo frontend interview questions

Reported in a public Google interview account. Google frontend interview questions

Reported in a public Netflix interview account. Netflix frontend interview questions

Reported in a public Zalando interview account. Zalando frontend interview questions

Reported in a public Booking.com interview account. Booking.com frontend interview questions