import { ref,onBeforeMount} from "vue"; /** * 倒计时 * @param {Number} second 倒计时秒数 * @return {Number} count 倒计时秒数 * @return {Function} countDown 倒计时函数 * @example * const { count, countDown } = useCountDown() * countDown(60) *
{{ count }}
*/ export function useCountDown() { const count = ref(0) const timer = ref(null); const countDown = (second = 60, ck = () => { }) => { if (count.value === 0 && timer.value === null) { ck(); count.value = second; timer.value = setInterval(() => { count.value-- if (count.value === 0) { clearInterval(timer.value) timer.value = null } }, 1000); } }; const cancelCout=()=>{ clearInterval(timer.value) timer.value = null count.value = 0 } onBeforeMount(() => { timer.value && clearInterval(timer.value) }); return { count, countDown, cancelCout }; }