Skip to content
On this page

debouncedWatch

Category
Last Changed
3 months ago

Debounced watch

Demo

Delay is set to 1000ms for this demo.

Input:

Times Updated: 0

Usage

Similar to watch, but offering an extra option debounce which will be applied to the callback function.

import { debouncedWatch } from '@vueuse/core'

debouncedWatch(
  source,
  () => { console.log('changed!') },
  { debounce: 500 }
)
import { debouncedWatch } from '@vueuse/core'

debouncedWatch(
  source,
  () => { console.log('changed!') },
  { debounce: 500 }
)

It's essentially a shorthand for the following code:

import { watchWithFilter, debounceFilter } from '@vueuse/core'

watchWithFilter(
  source,
  () => { console.log('changed!') },
  {
    eventFilter: debounceFilter(500),
  }
)
import { watchWithFilter, debounceFilter } from '@vueuse/core'

watchWithFilter(
  source,
  () => { console.log('changed!') },
  {
    eventFilter: debounceFilter(500),
  }
)

Type Declarations

export interface DebouncedWatchOptions<Immediate>
  extends WatchOptions<Immediate> {
  debounce?: MaybeRef<number>
}
export declare function debouncedWatch<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false
>(
  sources: T,
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: DebouncedWatchOptions<Immediate>
): WatchStopHandle
export declare function debouncedWatch<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: DebouncedWatchOptions<Immediate>
): WatchStopHandle
export declare function debouncedWatch<
  T extends object,
  Immediate extends Readonly<boolean> = false
>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: DebouncedWatchOptions<Immediate>
): WatchStopHandle
export interface DebouncedWatchOptions<Immediate>
  extends WatchOptions<Immediate> {
  debounce?: MaybeRef<number>
}
export declare function debouncedWatch<
  T extends Readonly<WatchSource<unknown>[]>,
  Immediate extends Readonly<boolean> = false
>(
  sources: T,
  cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
  options?: DebouncedWatchOptions<Immediate>
): WatchStopHandle
export declare function debouncedWatch<
  T,
  Immediate extends Readonly<boolean> = false
>(
  source: WatchSource<T>,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: DebouncedWatchOptions<Immediate>
): WatchStopHandle
export declare function debouncedWatch<
  T extends object,
  Immediate extends Readonly<boolean> = false
>(
  source: T,
  cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
  options?: DebouncedWatchOptions<Immediate>
): WatchStopHandle

Source

SourceDemoDocs

Contributors

Anthony Fu
wheat

Changelog

v4.6.4 on 4/4/2021
04dd5 - feat(filters): use number/ref with debounce & throttle filters (#410)
debouncedWatch has loaded