Debounce a function
npm install debounce-fn
import debounceFunction from 'debounce-fn';
window.onresize = debounceFunction(() => {
// Do something on window resize
}, {wait: 100});
Returns a debounced function that delays calling the input function until after wait milliseconds have elapsed since the last time the debounced function was called.
It comes with a .cancel() method to cancel any scheduled input function calls.
Type: Function
Function to debounce.
Type: object
Type: number
Default: 0
Time in milliseconds to wait until the input function is called.
Type: number
Default: Infinity
The maximum time the input function is allowed to be delayed before it is invoked.
This can be used to control the rate of calls handled in a constant stream. For example, a media player may send updates every few milliseconds, but you only want to handle them once a second.
Type: boolean
Default: false
Trigger the function on the leading edge of the wait interval.
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
Type: boolean
Default: true
Trigger the function on the trailing edge of the wait interval.
maxWait and before/after options.