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
Parameter | Type | Description |
---|---|---|
cb | (โฆargs: [โฆT]) โ V | The function to call. |
timeout | number | (Optional) The timeout to wait, in milliseconds |
resetTimer | boolean | (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.