R
fn

debounce

v0.0.10tested

Delays 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 AnyFunction

Parameters

ParameterTypeDescription
fnTThe function to debounce
waitnumber | (() => number)Milliseconds to wait, or a getter resolved lazily on each call (useful for reactive delays)
options?DebounceOptionsLeading/trailing edge behavior and `maxWait`

Returns

DebouncedFunction<T>The debounced function with `cancel()`, `flush()` and `pending()`