vhook

useTimeoutFn


用于在一段时间后执行回调

API

const { start, stop } = useTimeoutFn(fn, delay, immediate, clearEffectWhenStop)

Params

参数名描述类型默认值
fn回调Function
delay定时时间number
immediate是否立即注册定时器boolentrue
clearEffectWhenStop调用 stop 时是否停止内部的 watch 监听booleanfalse

Result

参数名描述类型
start开启/重启定时器() => void
Stop停止定时器() => void

Example

Code

<template>
  <p>Is callback executed?: {{executed}}</p>
  <button @click="start">restart</button>
  <button @click="stop">stop</button>
</template>
<script>
  import { ref } from 'vue'
  import {useTimeoutFn} from 'vhook'
  export default {
    setup () {
      const executed = ref(false)
      const { start, stop } = useTimeoutFn(() => {
        executed.value = true
      })
      return {
        executed,
        start: () => {
          executed.value = false
          start()
        },
        stop
      }
    }
  }
</script>