Skip to content

debounce

Code location

debounce<T, V>(cb, timeout?, resetTimer?): Debouncer<T, V>

A standard debounce function. Use this to have a time-delayed function only be called once in a given timeframe.

Type Parameters

T extends unknown[]

V

Parameters

cb

The function to call.

timeout?: number

The timeout to wait, in milliseconds

resetTimer?: boolean

Whether to reset the timeout when the debouncer is called again.

Returns

Debouncer<T, V>

a debounced function that takes the same parameter as the original function.

Example

const debounced = debounce((text: string) => {
console.log(text);
}, 1000, true);
debounced('Hello world'); // this will not be printed
await sleep(500);
debounced('World, hello'); // this will be printed to the console.