Defined in: debouncer.ts:118
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.
State Management:
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:125
TFn
DebouncerOptions<TFn>
Debouncer<TFn>
options: DebouncerOptions<TFn>;
options: DebouncerOptions<TFn>;
Defined in: debouncer.ts:122
readonly store: Store<Readonly<DebouncerState<TFn>>>;
readonly store: Store<Readonly<DebouncerState<TFn>>>;
Defined in: debouncer.ts:119
cancel(): void
cancel(): void
Defined in: debouncer.ts:242
Cancels any pending execution
void
flush(): void
flush(): void
Defined in: debouncer.ts:225
Processes the current pending execution immediately
void
maybeExecute(...args): void
maybeExecute(...args): void
Defined in: debouncer.ts:184
Attempts to execute the debounced function If a call is already in progress, it will be queued
...Parameters<TFn>
void
reset(): void
reset(): void
Defined in: debouncer.ts:253
Resets the debouncer state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: debouncer.ts:139
Updates the debouncer options
Partial<DebouncerOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.