简介
为传入的function
添加debounce
处理,并提供run
函数和cancel
函数
代码演示
基本使用
count is: 0
代码
<template>
<div class="text">count is: {{ count }}</div>
<button class="button" @click="run">click</button>
</template>
<script lang="ts">
import { ref, defineComponent } from 'vue'
import { useDebounceFn } from '@packages'
export default defineComponent({
name: 'UseDebounceFn',
setup: () => {
const count = ref(0)
const { run } = useDebounceFn(
() => {
count.value += 1
},
1000,
true
)
return { count, run }
}
})
</script>
<style scoped>
.text {
margin: 20px 0;
}
.button {
margin: 0 0 20px 0;
cursor: pointer;
}
</style>
API
const { run, cancel } = useDebounceFn(func, wait);
Retrun
参数 | 说明 | 类型 |
---|---|---|
run | 代理原函数的新debounce 函数 | function |
cancel | 在wait 时间到达之前,提供一个取消当前run 的方法 | function |
Params
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
func | 传入的函数 | function | - |
wait | 防抖的时间间隔 | number | 500ms |
immediate | 是否立即执行 | boolean | false |