tav-ui
Version:
50 lines (45 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var core = require('@vueuse/core');
function useCountdown(count) {
const currentCount = vue.ref(count);
const isStart = vue.ref(false);
let timerId;
function clear() {
timerId && window.clearInterval(timerId);
}
function stop() {
isStart.value = false;
clear();
timerId = null;
}
function start() {
if (vue.unref(isStart) || !!timerId) {
return;
}
isStart.value = true;
timerId = setInterval(() => {
if (vue.unref(currentCount) === 1) {
stop();
currentCount.value = count;
} else {
currentCount.value -= 1;
}
}, 1e3);
}
function reset() {
currentCount.value = count;
stop();
}
function restart() {
reset();
start();
}
core.tryOnUnmounted(() => {
reset();
});
return { start, reset, restart, clear, stop, currentCount, isStart };
}
exports.useCountdown = useCountdown;
//# sourceMappingURL=useCountdown2.js.map