Loading problem…
This problem builds on debounce-function. Complete that first, then load your solution to continue.
Implement an advanced debounce with cancellation, flushing, and optional immediate mode.
debounce(fn, delay, immediate = false).debounced(...args) with methods:debounced.cancel()debounced.flush()debounced returns undefined while work is pending.flush() immediately executes the pending call and returns its result.fn; they only extend the cooldown timer.cancel() clears any pending timer and pending invocation state.flush() returns undefined when nothing is pending (or when in immediate mode).this and latest arguments when invoking fn.delay = 0, execution must remain asynchronous in trailing mode.const d = debounce((x) => x * 2, 50)
d(1)
d(2)
d(3)
// after ~50ms => fn runs once with 3const d = debounce((x) => x + 1, 100)
d(4)
d.flush() // 5 (runs immediately)const d = debounce((x) => x, 100, true)
d("a") // "a" (immediate)
d("b") // undefined (suppressed)