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/refThrottled.md
1---2category: Reactivity3alias: useThrottle, throttledRef4---56# refThrottled78Throttle changing of a ref value.910## Usage1112```ts13import { refThrottled } from '@vueuse/core'14import { shallowRef } from 'vue'1516const input = shallowRef('')17const throttled = refThrottled(input, 1000)18```1920An example with object ref.2122```js23import { refThrottled } from '@vueuse/core'24import { shallowRef } from 'vue'2526const data = shallowRef({27count: 0,28name: 'foo',29})30const throttled = refThrottled(data, 1000)3132data.value = { count: 1, name: 'foo' }33console.log(throttled.value) // { count: 1, name: 'foo' } (immediate)3435data.value = { count: 2, name: 'bar' }36data.value = { count: 3, name: 'baz' }37data.value = { count: 4, name: 'qux' }38console.log(throttled.value) // { count: 1, name: 'foo' } (still first value)3940// After 1000ms, next change will be applied41await sleep(1100)42data.value = { count: 5, name: 'final' }43await nextTick()44console.log(throttled.value) // { count: 5, name: 'final' } (updated)45```4647### Trailing4849If you don't want to watch trailing changes, set 3rd param `false` (it's `true` by default):5051```ts52import { refThrottled } from '@vueuse/core'53import { shallowRef } from 'vue'5455const input = shallowRef('')56const throttled = refThrottled(input, 1000, false)57```5859### Leading6061Allows the callback to be invoked immediately (on the leading edge of the `ms` timeout). If you don't want this behavior, set the 4th param `false` (it's `true` by default):6263```ts64import { refThrottled } from '@vueuse/core'65import { shallowRef } from 'vue'6667const input = shallowRef('')68const throttled = refThrottled(input, 1000, undefined, false)69```7071## Recommended Reading7273- [Debounce vs Throttle: Definitive Visual Guide](https://kettanaito.com/blog/debounce-vs-throttle)74- [Debouncing and Throttling Explained Through Examples](https://css-tricks.com/debouncing-throttling-explained-examples/)7576## Type Declarations7778```ts79export type RefThrottledReturn<T = any> = Ref<T>80/**81* Throttle execution of a function. Especially useful for rate limiting82* execution of handlers on events like resize and scroll.83*84* @param value Ref value to be watched with throttle effect85* @param delay A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.86* @param trailing if true, update the value again after the delay time is up87* @param leading if true, update the value on the leading edge of the ms timeout88*/89export declare function refThrottled<T = any>(90value: Ref<T>,91delay?: number,92trailing?: boolean,93leading?: boolean,94): RefThrottledReturn<T>95/** @deprecated use `refThrottled` instead */96export declare const throttledRef: typeof refThrottled97/** @deprecated use `refThrottled` instead */98export declare const useThrottle: typeof refThrottled99```100