Note: This is an advanced version of Debounce, you should complete that first before attempting this question.
Debouncing is a technique used to control how many times we allow a function to be executed over time. When a JavaScript function is debounced with a wait time of X milliseconds, it must wait until after X milliseconds have elapsed since the debounced function was last called.
You almost certainly have encountered debouncing in your daily lives before (e.g. when entering an elevator). Only after X duration of not pressing the "Door open" button (the debounced function not being called) will the elevator door actually close (the callback function is executed).
Implement a debounce function which accepts a callback function and a wait duration. Calling debounce() returns a function which has debounced invocations of the callback function following the behavior described above.
Additionally, the debounce-ed function comes with two extra methods:
cancel() method to cancel pending invocationsflush() method to immediately invoke any delayed invocationslet i = 0;
function increment() {
i++;
}
const debouncedIncrement = debounce(increment, 100);
// t = 0: Call debouncedIncrement().
debouncedIncrement(); // i = 0
// t = 50: Cancel the delayed increment.
debouncedIncrement.cancel();
// t = 100: increment() was not invoked and i is still 0.
Flushing to instantly call the debounced function.
let i = 0;
function increment() {
i++;
}
const debouncedIncrement = debounce(increment, 100);
// t = 0: Call debouncedIncrement().
debouncedIncrement(); // i = 0
// t = 50: i is still 0 because 100ms have not passed.
// t = 51:
debouncedIncrement.flush(); // i is now 1 because flush causes() the callback to be immediately invoked.
// t = 100: i is already 1. The callback has been called before
// and won't be called again.
Note: Due to the use of delays in the tests for this question, tests can take a while to complete execution.
Below is a complete explanation of the debounce problem, your solution, and a fully commented version of the code.
Your solution is excellent — it correctly handles:
cancel() logic