Loading problem…
Implement debounce - a utility that delays function execution until calls stop for a fixed time window.
fn and a delay delay in milliseconds.debounced(...args).debounced resets the timer.fn.fn after delay milliseconds using the latest arguments.fn runs, preserve the caller's this value.delay of 0 must still schedule asynchronously (next macrotask).const calls = []
const debounced = debounce((value) => calls.push(value), 30)
debounced("a")
debounced("b")
debounced("c")
// After ~30ms: calls is ["c"]const obj = {
base: 10,
add: debounce(function (x) {
this.result = this.base + x
}, 20),
}
obj.add(5)
// After ~20ms: obj.result is 15const order = []
const debounced = debounce(() => order.push("later"), 0)
order.push("start")
debounced()
order.push("end")
// After next task: order is ["start", "end", "later"]