Problem Statement
The default throttle behavior executes on the leading edge—immediately when called. However, explicitly understanding this behavior is crucial for building more configurable solutions. Implement a leading-edge throttle with clear semantics.
Requirements
Implement a throttleLeading(func, wait) function that:
- Executes immediately on the first call (leading edge)
- Ignores all calls within the
waitperiod - Does NOT execute on the trailing edge (last queued call is dropped)
- Resets the timer only when a call actually executes
Example Usage
Visual Timeline
Use Cases
- Button click protection (instant feedback, prevent rapid clicks)
- Keyboard shortcuts (respond immediately, ignore repeats)
- Touch event handling on mobile
Follow-up Questions
- What are the downsides of dropping the trailing call?
- When might you prefer leading-only vs leading+trailing?