I want a standard API for event throttling and debouncing
Submitted by Šime Vidas
Permalink https://webwewant.fyi/wants/4/
There are numerous events in JavaScript that can get triggered repeatedly, necessitating that developers write additional code to throttle these events. For example:
- The Network Information API
change
event fires whenever a network change occurs, but this can happen in rapid succession when users pass through tunnels or move from WiFi to a cellular network. In almost all use cases, only the last event in the series is truly relevant. - The
resize
event is fired repeatedly when a window corner is dragged to resize the browser application. In almost all cases, a developer who wants to know when a resize has occurred—so they can adjust the user experience in a responsive context—only cares about the final event. - The
change
event on aninput
fires repeatedly when a user is typing, but for expensive operations like API lookups based on the entered content, developers will only want to trigger the API call when the user pauses for a bit or stops typing altogether. - The
mousemove
event fires much faster than movement is perceivable by the human eye, so it would be useful to be able to throttle it to a lower frequency (e.g., 20 Hz).
I want a native way to configure these (and similar) events to fire less frequently without having to write the debouncing and throttling code myself.
- This was presented at
- An Event Apart
–
Washington DC, USA
- An Event Apart