zjk
2026-07-17 47f583f707d9177486af3ac5c3c7844513691ca1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 }
}