The function to throttle
Throttle configuration options
A throttled function with cancel capability
const throttledFn = throttle(fn, {
delay: 1000,
onThrottle: (args) => {
console.log('throttled', args);
},
});
throttledFn(); // calls fn immediately
throttledFn(); // returns cached result
await wait(500);
throttledFn(); // returns cached result
// Cancel clears the throttle state
throttledFn.cancel();
throttledFn(); // calls fn immediately (state reset)
Throttle a function, calling it at most once every
delaymilliseconds. The first call is executed immediately, subsequent calls within the delay window return the cached result from the last execution.