debounce()
Search after typing pauses.
Try the example
Search this local reading list. Type “design” and watch the list update after you stop typing for 300 ms. The counter shows how much filtering actually ran.
- Designing a useful empty state
- Accessible menus from the start
- Small details in product design
- A guide to loading feedback
4 articles · 0 filter runs
Add it to your project
- Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
- Connect the behavior. Copy the JavaScript tab and call its exported
mount(element)after your component’s elements exist. Replace simulated delays and data operations with your own application code. - Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.
API and lifecycle
Wrap an expensive callback, then call the returned function with the latest input. Repeated calls restart the timer; the callback receives the most recent arguments and receiver. The default delay is 250 ms.
- Interface
fn, delay; cancel, flush- Lifecycle
- cancel() drops queued work. flush() runs queued work immediately. Cancel pending work when the component unmounts to avoid updating a removed view.
Accessibility and safety
Implementation notes
Initialize after the DOM is available. Keep native controls and keyboard interactions intact; release listeners and timers on unmount.
Use this for search or draft updates. Keep typing, focus, validation recovery and critical submit feedback immediate.
What to check
Type several characters quickly and confirm one callback runs after the pause. Unmount before the delay ends and confirm the callback does not run.