R
fn

watchThrottled

v0.0.15testeddemo

Like `watch`, but throttles the callback so it fires at most once per interval.

Examples

ts
const count = ref(0);
watchThrottled(count, value => console.log(value), { throttle: 500 });
ts
watchThrottled([a, b], ([a, b]) => save(a, b), { throttle: 1000, leading: false });

Demo

Loading demo…

Signatures

ts
export function watchThrottled<T, Immediate extends Readonly<boolean> = false>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions<Immediate>,
): WatchHandle;
ts
export function watchThrottled<T extends Readonly<MultiWatchSources>, Immediate extends Readonly<boolean> = false>(
  source: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: WatchThrottledOptions<Immediate>,
): WatchHandle;
ts
export function watchThrottled<T extends object, Immediate extends Readonly<boolean> = false>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: WatchThrottledOptions<Immediate>,
): WatchHandle;

Parameters

ParameterTypeDescription
sourceobject | WatchSource<unknown> | MultiWatchSourcesThe reactive source (ref, getter, reactive object, or array of sources) to watch
cbWatchCallbackInvoked with the new value, old value, and `onCleanup`, throttled by the interval
options?WatchThrottledOptions<boolean>Watch options plus `throttle` (ms), `leading`, and `trailing`

Returns

WatchHandleA handle to stop watching