Overview
debounce(fn, wait) delays calling fn until wait ms have passed without another call — ideal for search inputs and resize handlers. throttle(fn, limit) ensures fn fires at most once per limit ms — ideal for scroll and mousemove handlers.
Both return the wrapped function with two extra methods: .cancel() discards any pending call, and .flush() executes it immediately. They are plain utilities — not component-specific — so you can create them at module level or in a component's outer function.
debounce vs throttle
|
Utility
|
Fires when…
|
Best for
|
|---|---|---|
debounce
|
After activity stops for wait ms
| Search fields, resize handlers, save-on-type — wait until the user pauses. |
throttle
|
At most once per limit ms
| Scroll position, mousemove, analytics — sample continuous events periodically. |
debounce + throttle, plus cancel() / flush()
Move the mouse: debounce waits for idle, throttle samples. Type in the search field then hit search.cancel() to drop the pending call, or search.flush() to run it immediately.