91 lines
1.7 KiB
TypeScript
91 lines
1.7 KiB
TypeScript
import { onMounted, ref } from 'vue'
|
|
import moment from 'moment'
|
|
//sample 00:30 - 1:50 - 00:59
|
|
export default function useCountdownTimer(_time = '00:00:30', format = 'HH:mm:ss') {
|
|
let interval: number
|
|
let startTime = moment(_time, 'HH:mm:ss')
|
|
//const isActiveMenu=
|
|
const endTime = moment('00:00:00', 'HH:mm:ss')
|
|
const time = ref('')
|
|
let tempHook: () => void
|
|
let tempHookStop: () => void
|
|
let tempHookStart: () => void
|
|
let tempHookRestart: () => void
|
|
function decrement() {
|
|
if (startTime > endTime) {
|
|
startTime.subtract('1', 'second')
|
|
time.value = startTime.format(format)
|
|
} else {
|
|
clearInterval(interval)
|
|
if (tempHook) {
|
|
tempHook()
|
|
}
|
|
}
|
|
}
|
|
|
|
function start() {
|
|
if (interval) {
|
|
stop()
|
|
}
|
|
interval = setInterval(decrement, 1000)
|
|
if (tempHookStart) {
|
|
tempHookStart()
|
|
}
|
|
}
|
|
|
|
function stop() {
|
|
clearInterval(interval)
|
|
if (tempHookStop) {
|
|
tempHookStop()
|
|
}
|
|
}
|
|
|
|
function onFinish(hook: () => any) {
|
|
tempHook = hook
|
|
}
|
|
|
|
function setTime(_time = '00:00:30') {
|
|
startTime = moment(_time, 'HH:mm:ss')
|
|
time.value = startTime.format(format)
|
|
stop()
|
|
start()
|
|
}
|
|
|
|
function reset() {
|
|
startTime = moment(_time, 'HH:mm:ss')
|
|
time.value = startTime.format(format)
|
|
stop()
|
|
start()
|
|
if (tempHookRestart) {
|
|
tempHookRestart()
|
|
}
|
|
}
|
|
|
|
function onStop(hook: () => any) {
|
|
tempHookStop = hook
|
|
}
|
|
function onStart(hook: () => any) {
|
|
tempHookStart = hook
|
|
}
|
|
|
|
function onRestart(hook: () => any) {
|
|
tempHookRestart = hook
|
|
}
|
|
|
|
onMounted(() => {
|
|
time.value = startTime.format(format)
|
|
})
|
|
|
|
return {
|
|
time,
|
|
stop,
|
|
start,
|
|
onFinish,
|
|
reset,
|
|
setTime,
|
|
onRestart,
|
|
onStart,
|
|
onStop,
|
|
}
|
|
}
|