Implement Throttle

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 Throttle

Approach

Track the timestamp of the last execution. On each call, check whether enough time has elapsed since the last run. If so, execute the function and update the timestamp; otherwise, skip the call. Use apply to preserve context.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Throttle Pattern

Why It Works

Comparing Date.now() against the last execution time guarantees the function runs at most once per interval, smoothing rapid event streams like scroll or resize.

Updated Mar 2026