Defined in: debouncer.ts:142
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. This synchronous version is lighter weight and often all you need - upgrade to AsyncDebouncer when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
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:150
TFn
DebouncerOptions<TFn>
Debouncer<TFn>
fn: TFn;
fn: TFn;
Defined in: debouncer.ts:151
key: string;
key: string;
Defined in: debouncer.ts:146
options: DebouncerOptions<TFn>;
options: DebouncerOptions<TFn>;
Defined in: debouncer.ts:147
readonly store: Store<Readonly<DebouncerState<TFn>>>;
readonly store: Store<Readonly<DebouncerState<TFn>>>;
Defined in: debouncer.ts:143
_emit(): void
_emit(): void
Defined in: debouncer.ts:171
Emits a change event for the debouncer instance. Mostly useful for devtools.
void
cancel(): void
cancel(): void
Defined in: debouncer.ts:286
Cancels any pending execution
void
flush(): void
flush(): void
Defined in: debouncer.ts:269
Processes the current pending execution immediately
void
maybeExecute(...args): void
maybeExecute(...args): void
Defined in: debouncer.ts:222
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:297
Resets the debouncer state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: debouncer.ts:176
Updates the debouncer options
Partial<DebouncerOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.