useInterval

useInterval 定时器执行一个函数

基础用法


<template>
  <div>Hello World! {{ count }}</div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useInterval } from "hooks-vue3";
const count = ref<number>(0);
useInterval(() => {
  count.value++;
}, 3000);
</script>

API


useInterval(() => void, time:number);

参数TS类型

export type UseIntervalFnReturn = {
  isRunning: Ref<boolean>;
  start: () => void; //开始定时器
  stop: () => void; //停止定时器
};

export type UseIntervalFnOptions = {
  immediate?: boolean;
  autoStart?: boolean;
};

function useInterval(
  callback: () => void,
  delay = 0,
  options?: UseIntervalFnOptions,
): UseIntervalFnReturn

Last updated