Skip to content

useThrottle

Category
Last Changed
3 months ago

Throttle changing of a ref value.

Demo

Delay is set to 1000ms for this demo.

Throttled:

Times Updated: 0

Trailing: true

Leading: false

Usage

import { useThrottle } from '@vueuse/core'

const input = ref('')
const throttled = useThrottle(input, 1000)
import { useThrottle } from '@vueuse/core'

const input = ref('')
const throttled = useThrottle(input, 1000)

Trailing

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

import { useThrottle } from '@vueuse/core'

const input = ref('')
const throttled = useThrottle(input, 1000, false)
import { useThrottle } from '@vueuse/core'

const input = ref('')
const throttled = useThrottle(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 begavior, set 4rd param false (it's true by default):

import { useThrottle } from '@vueuse/core'

const input = ref('')
const throttled = useThrottle(input, 1000, undefined, false)
import { useThrottle } from '@vueuse/core'

const input = ref('')
const throttled = useThrottle(input, 1000, undefined, false)
  • useThrottle
  • useThrottleFn
  • useDebounce
  • useDebounceFn

Type Declarations

/**
 * 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=true] if true, update the value again after the delay time is up
 * @param [leading=true] if true, update the value on the leading edge of the ms timeout
 */
export declare function useThrottle<T>(
  value: Ref<T>,
  delay?: number,
  trailing?: boolean,
  leading?: boolean
): 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=true] if true, update the value again after the delay time is up
 * @param [leading=true] if true, update the value on the leading edge of the ms timeout
 */
export declare function useThrottle<T>(
  value: Ref<T>,
  delay?: number,
  trailing?: boolean,
  leading?: boolean
): Ref<T>

Source

SourceDemoDocs

Contributors

Anthony Fu
Jakub Freisler
Roman Harmyder
Nurettin Kaya

Changelog

v6.5.0 on 9/25/2021
c0078 - feat(throttleFilter): add leading option (#771)
v6.4.0 on 9/17/2021
4a33e - feat: trailing option, useThrottledRefHistory updated (#760)
useThrottle has loaded