This question evaluates a candidate's ability to implement a debounce utility using runtime features such as closures, timers, and function-context preservation, testing competencies in function design, stateful behavior, and event-rate control within the Coding & Algorithms domain.
debounce(fn, wait)Implement a debounce utility function.
Given a function fn and a delay wait (milliseconds), debounce(fn, wait) returns a new function debounced such that:
debounced(...args)
is called repeatedly, it postpones calling
fn
until
after
wait
ms have elapsed since the
most recent
call.
fn
is invoked
once
with the
latest
arguments and the original
this
context.
this
binding when invoking
fn
.
debounced.cancel()
to cancel a pending invocation.
debounced.flush()
to immediately run the pending invocation (if any).