Debounce with Leading Edge in JavaScript
Debouncing is a common technique used to limit the rate at which a function is executed. This is particularly useful for handling events like window resizing, scrolling, or key presses, where you don't want to trigger a function on every single event, but rather only after a period of inactivity. This challenge asks you to implement a debounced function with a "leading edge" trigger, meaning the function will execute immediately on the first call within the debounce window, and then only execute again after the debounce period has elapsed since the last call.
Problem Description
You are tasked with creating a debounceWithLeadingEdge function that takes a function (func), a delay (wait), and an optional immediate flag (defaults to true) as arguments. The debounceWithLeadingEdge function should return a new function that, when called, will delay invoking func unless it's called repeatedly faster than wait milliseconds. The leading edge behavior means that the first call within the debounce window will immediately execute func, and subsequent calls within the window will be ignored until the window expires. After the window expires, the next call will again trigger immediate execution.
Key Requirements:
- The returned function should accept the same arguments as
func. funcshould be invoked at most once during thewaitperiod.- The first call within the
waitperiod should immediately invokefunc(leading edge). - Subsequent calls within the
waitperiod should be ignored. - After the
waitperiod has elapsed since the last call, the next call should immediately invokefunc.
Expected Behavior:
The debounced function should act as a gatekeeper, controlling the execution of the original function based on the specified delay and leading edge behavior.
Edge Cases to Consider:
waitvalue of 0: Should execute immediately on every call.- Rapidly called function: Ensure only the first call within the debounce window triggers execution.
- Function calls after the debounce window has expired.
funcbeing called with different arguments each time. The debounced function should pass these arguments tofunc.
Examples
Example 1:
Input:
func = (x) => console.log("Executing with:", x);
wait = 500;
debouncedFunc = debounceWithLeadingEdge(func, wait);
debouncedFunc(1); // Executes immediately: "Executing with: 1"
debouncedFunc(2); // Ignored
debouncedFunc(3); // Ignored
setTimeout(() => debouncedFunc(4), 600); // Executes after 600ms: "Executing with: 4"
Explanation: The first call with 1 executes immediately. The calls with 2 and 3 are ignored because they occur within the 500ms window. After 600ms (more than the 500ms wait), the call with 4 executes.
Example 2:
Input:
func = () => console.log("Executing");
wait = 200;
debouncedFunc = debounceWithLeadingEdge(func, wait);
debouncedFunc(1); // Executes immediately: "Executing"
debouncedFunc(2); // Ignored
setTimeout(() => debouncedFunc(3), 300); // Executes after 300ms: "Executing"
Explanation: The first call executes immediately. The call after 300ms executes because the 200ms window has expired.
Example 3: (Edge Case - wait = 0)
Input:
func = (x) => console.log("Executing with:", x);
wait = 0;
debouncedFunc = debounceWithLeadingEdge(func, wait);
debouncedFunc(1); // Executes immediately: "Executing with: 1"
debouncedFunc(2); // Executes immediately: "Executing with: 2"
debouncedFunc(3); // Executes immediately: "Executing with: 3"
Explanation: With a wait time of 0, the function executes immediately on every call.
Constraints
waitmust be a non-negative number.funcmust be a function.- The debounced function should handle any number of arguments passed to it.
- The time complexity of the debounce function itself should be O(1).
- The space complexity of the debounce function should be O(1).
Notes
- Consider using
setTimeoutto implement the delay. - Think about how to track the last call time and the timeout.
- The
thiscontext of the original function should be preserved when it's invoked. - Pay close attention to the leading edge behavior – the first call should always trigger execution.
- Remember to handle the case where
waitis 0 correctly.