likes
comments
collection
share

Vueuse中的 useIntervalFn 使用

作者站长头像
站长
· 阅读数 93

Vue3项目中用来计数的功能, 项目中经常要用到倒计时功能. 这里借助vueuse库 进行了二次封装

    function useCountDown() {
  const counter = ref(0)
  const { pause, resume } = useIntervalFn(() => {
    counter.value--
    if (counter.value === 0) {
      pause() // 停止定时器
    }
  }, 1000, {immediate: false})
  const start = (n: number) => {
    counter.value = n
    resume() // 开启定时器
  } 
  return { counter, start } // 返回counter 和  start函数
}