call(back)

Debounce

Implement debounce(fn, wait): return a wrapped function that delays invoking fn until wait milliseconds have passed since the last call. Support a cancel() method that drops any pending invocation, and be ready to explain how debounce differs from throttle and when a leading-edge option matters.

The grader replays timed call scripts against your implementation on a virtual clock. Each case's input is the wait plus a script of [ms, action, ...args] steps; the expected output lists every fire as {at, args}. Forward arguments faithfully.

Hints

Hint 1

Each call should reset the pending timer — a closure over the timer id is all the state you need.

Hint 2

For leading-edge behavior, fire immediately when no timer is pending, then suppress calls until things go quiet.

Asked at