debounce

debounce() function

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

Signature:

export function debounce<T extends unknown[], V>(cb: (...args: [...T]) => V, timeout?: number, resetTimer?: boolean): Debouncer<T, V>;

Parameters

ParameterTypeDescription
cb(โ€ฆargs: [โ€ฆT]) โ‡’ VThe function to call.
timeoutnumber(Optional) The timeout to wait, in milliseconds
resetTimerboolean(Optional) 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.