import dayjs from 'dayjs'
|
|
/**
|
* 实时时钟 composable,每秒更新当前时间
|
* @returns now - 格式化的当前时间字符串(YYYY-MM-DD HH:mm:ss)
|
*/
|
export const useClock = () => {
|
const now = ref(dayjs().format('YYYY-MM-DD HH:mm:ss'))
|
let timer: ReturnType<typeof setInterval> | null = null
|
|
onMounted(() => {
|
timer = setInterval(() => {
|
now.value = dayjs().format('YYYY-MM-DD HH:mm:ss')
|
}, 1000)
|
})
|
|
onUnmounted(() => {
|
if (timer) {
|
clearInterval(timer)
|
timer = null
|
}
|
})
|
|
return { now }
|
}
|