← 返回 apple 的题目列表Implement Debounce Function
类型:online_judge
Implement debounce(fn, wait): return a new function debounced such that:
When debounced is called repeatedly, fn is invoked only once after there have been no calls for wait milliseconds (trailing debounce).
Preserve this binding and forward all arguments to fn.
The returned debounced must provide:
debounced.cancel(): cancels any pending invocation
debounced.flush(): if an invocation is pending, invoke immediately, clear the timer, and return fn's return value; otherwise return undefined
Requirements
Use JavaScript (browser or Node.js).
Clarify the timing behavior: multiple calls result in only the last call being executed.
Example (pseudo)
wait = 100
Calls at t=0, 30, 60 to debounced(1), debounced(2), debounced(3) should invoke fn once around t=160 with argument 3.