Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Latest commit

 

History

History
23 lines (16 loc) · 903 Bytes

debounce.mdx

File metadata and controls

23 lines (16 loc) · 903 Bytes

export const metadata = { eventPayloads: { GO: { action: () => { alert('Action fired!'); }, }, }, };

Debounce

Debouncing is a handy tool for lightening the load on heavy tasks. If you have a search box that you want to use for filtering on the server, debouncing will help you deduplicate multiple calls.

Debouncing can be troublesome to implement in vanilla Javascript, but state machines make them quite trivial.

Pressing Go

We start off in the idle state. Pressing GO will send us into debouncing.

From there, we have a countdown of two seconds before the action we passed into the event is fired. Which in this case, is alert('Action fired!').

But if we press GO multiple times, we stay in debouncing longer than two seconds, and only the last action we passed in gets fired.