Skip to content

refThrottled

类别
导出大小
536 B
最近修改
2 days ago
别名
useThrottlethrottledRef
相关

参考值的节流变化。

🌐 Throttle changing of a ref value.

示例

Delay is set to 1000ms for this demo.

Throttled:

Times Updated: 0

Trailing: true

Leading: false

用法

🌐 Usage

ts
import { 
refThrottled
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
input
=
shallowRef
('')
const
throttled
=
refThrottled
(
input
, 1000)

一个使用对象引用的示例。

🌐 An example with object ref.

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

const data = shallowRef({
  count: 0,
  name: 'foo',
})
const throttled = refThrottled(data, 1000)

data.value = { count: 1, name: 'foo' }
console.log(throttled.value) // { count: 1, name: 'foo' } (immediate)

data.value = { count: 2, name: 'bar' }
data.value = { count: 3, name: 'baz' }
data.value = { count: 4, name: 'qux' }
console.log(throttled.value) // { count: 1, name: 'foo' } (still first value)

// After 1000ms, next change will be applied
await sleep(1100)
data.value = { count: 5, name: 'final' }
await nextTick()
console.log(throttled.value) // { count: 5, name: 'final' } (updated)

尾随

🌐 Trailing

如果你不想查看后续的更改,请设置第3个参数 false(默认是 true):

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

ts
import { 
refThrottled
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
input
=
shallowRef
('')
const
throttled
=
refThrottled
(
input
, 1000, false)

领导

🌐 Leading

允许回调立即被调用(在 ms 超时的开始时刻)。如果你不希望这种行为,请设置第四个参数 false(默认是 true):

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

ts
import { 
refThrottled
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
input
=
shallowRef
('')
const
throttled
=
refThrottled
(
input
, 1000,
undefined
, false)

🌐 Recommended Reading