Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/watchThrottled.md
1---2category: Watch3alias: throttledWatch4---56# watchThrottled78Throttled watch. The callback will be invoked at most once per specified duration.910## Usage1112Similar to `watch`, but offering extra options `throttle`, `trailing`, and `leading` which will be applied to the callback function.1314```ts15import { watchThrottled } from '@vueuse/core'1617watchThrottled(18source,19() => { console.log('changed!') },20{ throttle: 500 },21)22```2324### Options2526| Option | Type | Default | Description |27| ---------- | -------------------------- | ------- | ----------------------------------------- |28| `throttle` | `MaybeRefOrGetter<number>` | `0` | Throttle interval in ms (can be reactive) |29| `trailing` | `boolean` | `true` | Invoke on the trailing edge |30| `leading` | `boolean` | `true` | Invoke on the leading edge |3132All standard `watch` options (`deep`, `immediate`, `flush`, etc.) are also supported.3334### Leading and Trailing3536Control when the callback is invoked:3738```ts39import { watchThrottled } from '@vueuse/core'4041// Only invoke at the start of each throttle period42watchThrottled(source, callback, {43throttle: 500,44leading: true,45trailing: false,46})4748// Only invoke at the end of each throttle period49watchThrottled(source, callback, {50throttle: 500,51leading: false,52trailing: true,53})54```5556## How It Works5758It's essentially a shorthand for the following code:5960```ts61import { throttleFilter, watchWithFilter } from '@vueuse/core'6263watchWithFilter(64source,65() => { console.log('changed!') },66{67eventFilter: throttleFilter(500),68},69)70```7172## Type Declarations7374```ts75export interface WatchThrottledOptions<76Immediate,77> extends WatchOptions<Immediate> {78throttle?: MaybeRefOrGetter<number>79trailing?: boolean80leading?: boolean81}82export declare function watchThrottled<83T extends Readonly<MultiWatchSources>,84Immediate extends Readonly<boolean> = false,85>(86sources: [...T],87cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,88options?: WatchThrottledOptions<Immediate>,89): WatchHandle90export declare function watchThrottled<91T,92Immediate extends Readonly<boolean> = false,93>(94source: WatchSource<T>,95cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,96options?: WatchThrottledOptions<Immediate>,97): WatchHandle98export declare function watchThrottled<99T extends object,100Immediate extends Readonly<boolean> = false,101>(102source: T,103cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,104options?: WatchThrottledOptions<Immediate>,105): WatchHandle106/** @deprecated use `watchThrottled` instead */107export declare const throttledWatch: typeof watchThrottled108```109