Defined in: async-debouncer.ts:71
A class that creates an async debounced function.
Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation. Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing or input changes where you only want to execute the handler after the events have stopped occurring.
Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until the function stops being called for the specified delay period.
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
await searchAPI(value);
}, { wait: 500 });
// Called on each keystroke but only executes after 500ms of no typing
inputElement.addEventListener('input', () => {
asyncDebouncer.maybeExecute(inputElement.value);
});
const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
await searchAPI(value);
}, { wait: 500 });
// Called on each keystroke but only executes after 500ms of no typing
inputElement.addEventListener('input', () => {
asyncDebouncer.maybeExecute(inputElement.value);
});
• TFn extends AnyAsyncFunction
• TArgs extends Parameters<TFn>
new AsyncDebouncer<TFn, TArgs>(fn, initialOptions): AsyncDebouncer<TFn, TArgs>
new AsyncDebouncer<TFn, TArgs>(fn, initialOptions): AsyncDebouncer<TFn, TArgs>
Defined in: async-debouncer.ts:83
TFn
AsyncDebouncerOptions<TFn, TArgs>
AsyncDebouncer<TFn, TArgs>
cancel(): void
cancel(): void
Defined in: async-debouncer.ts:172
Cancels any pending execution
void
getExecutionCount(): number
getExecutionCount(): number
Defined in: async-debouncer.ts:188
Returns the number of times the function has been executed
number
getIsPending(): boolean
getIsPending(): boolean
Defined in: async-debouncer.ts:195
Returns true if there is a pending execution
boolean
getOptions(): Required<AsyncDebouncerOptions<TFn, TArgs>>
getOptions(): Required<AsyncDebouncerOptions<TFn, TArgs>>
Defined in: async-debouncer.ts:110
Returns the current debouncer options
Required<AsyncDebouncerOptions<TFn, TArgs>>
maybeExecute(...args): Promise<void>
maybeExecute(...args): Promise<void>
Defined in: async-debouncer.ts:118
Attempts to execute the debounced function If a call is already in progress, it will be queued
...TArgs
Promise<void>
setOptions(newOptions): Required<AsyncDebouncerOptions<TFn, TArgs>>
setOptions(newOptions): Required<AsyncDebouncerOptions<TFn, TArgs>>
Defined in: async-debouncer.ts:97
Updates the debouncer options Returns the new options state
Partial<AsyncDebouncerOptions<TFn, TArgs>>
Required<AsyncDebouncerOptions<TFn, TArgs>>
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.