fn
debounce
v0.0.10testedDelays invoking a function until `wait` ms have elapsed since the last call
Examples
ts
const onResize = debounce(() => layout(), 200);
window.addEventListener('resize', onResize);
onResize.cancel();ts
// Reactive delay + a guaranteed call at least every 1000ms under sustained input
const save = debounce(persist, () => delayRef.value, { maxWait: 1000 });Signature
ts
export function debounce<T extends AnyFunction>(
fn: T,
wait: number | (() => number),
options: DebounceOptions ={ ... }Type Parameters
Textends AnyFunctionParameters
| Parameter | Type | Description |
|---|---|---|
fn | T | The function to debounce |
wait | number | (() => number) | Milliseconds to wait, or a getter resolved lazily on each call (useful for reactive delays) |
options? | DebounceOptions | Leading/trailing edge behavior and `maxWait` |
Returns
DebouncedFunction<T>The debounced function with `cancel()`, `flush()` and `pending()`