useTimeout
延时器Hook
前置依赖
依赖参数
| 参数名 | 类型 | 说明 |
|---|---|---|
deps.vue.ref | <T>(value: T) => Ref<T> | Vue的ref函数 |
deps.vue.onUnmounted | (fn: () => void) => void | Vue的onUnmounted钩子 |
deps.setTimeout | typeof setTimeout | 设置延时函数 |
deps.clearTimeout | typeof clearTimeout | 清除延时函数 |
使用示例
typescript
import { ref, onUnmounted } from 'vue'
import { useTimeout } from 'zcw-shared/hooks/useTimeout'
const { isPending, isReady, start, stop } = useTimeout(
() => {
console.log('3秒后执行')
},
3000,
{ immediate: true },
{
vue: { ref, onUnmounted },
setTimeout,
clearTimeout
}
)
// 停止延时器
stop()
// 重新启动
start()