The function to throttle
The minimum time between function calls in milliseconds
A throttled function with a cancel method
Unlike debounce, throttle ensures the function is called at a regular interval during continuous invocations. The first call is executed immediately, and subsequent calls within the wait period are queued to execute at the end of the wait period.
const throttledUpdate = throttle((data) => {
broadcastUpdate(data);
}, 50); // Max 20 updates per second
// Called many times rapidly, but executes at most every 50ms
canvas.on('object:modified', () => throttledUpdate(canvas.toJSON()));
// Clean up on unmount
return () => throttledUpdate.cancel();
Creates a throttled version of a function that only executes at most once every
waitmilliseconds.