Skip to main content

debounce

@webda/utils


Function: debounce()

debounce<T>(func, wait, options?): DebouncedFunction<T>

Defined in: packages/utils/src/debounce.ts:83

Creates a debounced function that delays invoking the provided function until after wait milliseconds have elapsed since the last time the debounced function was invoked.

The debounced function comes with a cancel method to cancel delayed invocations, a flush method to immediately invoke them, and a pending method to check if the function is waiting to be invoked.

Type Parameters​

T​

T extends (...args) => any

Parameters​

func​

T

The function to debounce

wait​

number

The number of milliseconds to delay

options?​

DebounceOptions = {}

Additional debounce options

Returns​

DebouncedFunction<T>

Returns the new debounced function

Example​

const saveData = debounce(async (data: string) => {
await api.save(data);
}, 1000);

// Will only call once after 1 second of no calls
saveData('hello');
saveData('world');
saveData('!'); // Only this call will execute after 1s

// Cancel pending execution
saveData.cancel();

// Execute immediately
saveData.flush();