Expand description
interface DebounceConstructor<TFn> {
disregardExecutionTime?: boolean;
fn: TFn;
interval: number;
}Properties§
§§§
disregard Execution Time?: booleanBy default, the debouncer takes in account the time
the underlying function executes. i.e. the actual debounce
interval is max(interval, executionTime). This default
behavior guanrantees that no 2 calls will be executed concurrently.
If you want the debouncer to always debounce at the set interval, set this to true.
fn: TFnFunction to be debounced
interval: numberMinimum interval between each call
Setting this to <= 0 will make the debounce function a pure pass-through, not actually debouncing the function
Options for
debouncefunctionA debounced function is an async event wrapper that is guaranteed to:
The caller will get a promise that resolves the next time the event is fired and resolved.
Unlike the naive implementation with a setTimeout, this implementation will not starve the event. If it's constantly being called, it will keep firing the event at at least the minimum interval (might take longer if the underlying function takes longer to execute
Simple Example
Multiple calls will be debounced to the minimum interval
Discarding extra calls
When making multiple calls, if the call is currently being debounced (i.e. executed and the minimum interval hasn't passed), new calls will replace the previous call.
If you want to the in-between calls to be preserved, use
batchinstead.Slow function
By default, the debouncer takes into account the time it takes for the underlying function to execute. It starts the next cycle as soon as both the minimul interval has passed and the function has finished executing. This ensures only 1 call is being executed at a time.
However, if you want the debouncer to always debounce at the set interval, regardless of if the previous call has finished, set
disregardExecutionTimeto true.