Implement Debounce

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Debounce

Approach

Return a wrapper function that resets a timer on every call using clearTimeout and setTimeout. The original function only executes after the caller stops invoking for the specified wait period. Use apply to preserve the correct this context and arguments.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Debounce Pattern

Why It Works

Each invocation clears the previous timer and starts a new one, so only the last call in a burst of rapid calls actually triggers the function after the wait period elapses.

Updated Mar 2026