Files
seojng-tv/composables/useRoomTimer.ts
T
ysj7705 69b647145f
CI / Type-check & Build (push) Failing after 5m50s
CI / Build & Push image (push) Skipped
first commit
2026-08-12 15:01:43 +09:00

27 lines
790 B
TypeScript

import { useTimestamp } from '@vueuse/core'
export function useRoomTimer(startedAt: MaybeRefOrGetter<number | null>) {
const now = useTimestamp({ interval: 1000 })
const elapsedMs = computed(() => {
const start = toValue(startedAt)
if (!start) return 0
return Math.max(0, now.value - start)
})
const formatted = computed(() => formatDuration(elapsedMs.value))
return { elapsedMs, formatted }
}
export function formatDuration(ms: number) {
const totalSec = Math.floor(ms / 1000)
const h = Math.floor(totalSec / 3600)
const m = Math.floor((totalSec % 3600) / 60)
const s = totalSec % 60
if (h > 0) {
return `${h}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`
}
return `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`
}