Defined in: debouncer.ts:68
A class that creates a debounced function.
Debouncing ensures that a function is only executed after a certain amount of time has passed since its last invocation. This is useful for handling frequent events like window resizing, scroll events, or input changes where you want to limit the rate of execution.
The debounced function can be configured to execute either at the start of the delay period (leading edge) or at the end (trailing edge, default). Each new call during the wait period will reset the timer.
const debouncer = new Debouncer((value: string) => {
saveToDatabase(value);
}, { wait: 500 });
// Will only save after 500ms of no new input
inputElement.addEventListener('input', () => {
debouncer.maybeExecute(inputElement.value);
});
const debouncer = new Debouncer((value: string) => {
saveToDatabase(value);
}, { wait: 500 });
// Will only save after 500ms of no new input
inputElement.addEventListener('input', () => {
debouncer.maybeExecute(inputElement.value);
});
• TFn extends AnyFunction
new Debouncer<TFn>(fn, initialOptions): Debouncer<TFn>
new Debouncer<TFn>(fn, initialOptions): Debouncer<TFn>
Defined in: debouncer.ts:75
TFn
DebouncerOptions<TFn>
Debouncer<TFn>
cancel(): void
cancel(): void
Defined in: debouncer.ts:161
Cancels any pending execution
void
getEnabled(): boolean
getEnabled(): boolean
Defined in: debouncer.ts:108
Returns the current enabled state of the debouncer
boolean
getExecutionCount(): number
getExecutionCount(): number
Defined in: debouncer.ts:172
Returns the number of times the function has been executed
number
getIsPending(): boolean
getIsPending(): boolean
Defined in: debouncer.ts:179
Returns true if debouncing
boolean
getOptions(): Required<DebouncerOptions<TFn>>
getOptions(): Required<DebouncerOptions<TFn>>
Defined in: debouncer.ts:101
Returns the current debouncer options
Required<DebouncerOptions<TFn>>
getWait(): number
getWait(): number
Defined in: debouncer.ts:115
Returns the current wait time in milliseconds
number
maybeExecute(...args): void
maybeExecute(...args): void
Defined in: debouncer.ts:123
Attempts to execute the debounced function If a call is already in progress, it will be queued
...Parameters<TFn>
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: debouncer.ts:89
Updates the debouncer options Returns the new options state
Partial<DebouncerOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.