UNPKG

sinter-compositions

Version:
48 lines (39 loc) 995 B
import {Fn} from "../../types"; import {computed, ref} from 'vue' import {noop} from "../utils/helper"; // 倒计时 export function useCountDown(timer: number, callback ?: Fn<number>, { step = 1000, done = noop, immediate = false } = {}) { const current = ref(timer) let i: any = null function callDone() { if (i !== null) { clearTimeout(i) } done && done.call(null) } function run() { return setTimeout(() => { current.value -= step; if (current.value <= 0) { callDone() } else { callback && callback(current.value, step) i = run() } }, step) } const context = { run: () => { i = run() }, toSecond: computed(() => (current.value / 1000)) } if (immediate) { context.run() } return [current, context] as [typeof current, typeof context] }