Skip to content

refDebounced

参考值的去抖动执行。

¥Debounce execution of a ref value.

示例

Delay is set to 1000ms for this demo.

Debounced:

Times Updated: 0

用法

¥Usage

ts
import { 
refDebounced
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
input
=
shallowRef
('foo')
const
debounced
=
refDebounced
(
input
, 1000)
input
.
value
= 'bar'
console
.
log
(
debounced
.
value
) // 'foo'
await
sleep
(1100)
console
.
log
(
debounced
.
value
) // 'bar'

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

¥An example with object ref.

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

const data = shallowRef({
  name: 'foo',
  age: 18,
})
const debounced = refDebounced(data, 1000)

function update() {
  data.value = {
    ...data.value,
    name: 'bar',
  }
}

console.log(debounced.value) // { name: 'foo', age: 18 }
update()
await sleep(1100)

console.log(debounced.value) // { name: 'bar', age: 18 }

你还可以传递可选的第三个参数,包括 maxWait 选项。详情请参见 useDebounceFn

¥You can also pass an optional 3rd parameter including maxWait option. See useDebounceFn for details.

¥Recommended Reading