Skip to content

refThrottled

Category
Export Size
556 B
Last Changed
2 months ago
Alias
useThrottlethrottledRef
Related

Throttle changing of a ref value.

Demo

Delay is set to 1000ms for this demo.

Throttled:

Times Updated: 0

Trailing: true

Leading: false

Usage

js
import { refThrottled } from '@vueuse/core'
import { shallowRef } from 'vue'

const input = shallowRef('')
const throttled = refThrottled(input, 1000)

Trailing

If you don't want to watch trailing changes, set 3rd param false (it's true by default):

js
import { refThrottled } from '@vueuse/core'
import { shallowRef } from 'vue'

const input = shallowRef('')
const throttled = refThrottled(input, 1000, false)

Leading

Allows 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):

js
import { refThrottled } from '@vueuse/core'
import { shallowRef } from 'vue'

const input = shallowRef('')
const throttled = refThrottled(input, 1000, undefined, false)

Type Declarations

typescript
export type RefThrottledReturn<T = any> = Ref<T>
/**
 * Throttle execution of a function. Especially useful for rate limiting
 * execution of handlers on events like resize and scroll.
 *
 * @param value Ref value to be watched with throttle effect
 * @param  delay  A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
 * @param trailing if true, update the value again after the delay time is up
 * @param leading if true, update the value on the leading edge of the ms timeout
 */
export declare function refThrottled<T = any>(
  value: Ref<T>,
  delay?: number,
  trailing?: boolean,
  leading?: boolean,
): RefThrottledReturn<T>
export { refThrottled as throttledRef, refThrottled as useThrottle }

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
Robin
Thimo Sietsma
IlyaL
Danny Feliz

Changelog

v13.1.0 on
c1d6e - feat(shared): ensure return types exists (#4659)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.

Talks: June 3-4 Live Coding: June 5th
Join for Free ➜
01
days
:
08
hours
:
03
minutes
:
09
seconds
: