You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
let timerId: ReturnType<typeof setInterval> | null = null let startTime: number = 0
// 开始计时器
function startPosityveTimer(callback: (timeString: string) => void) { if (timerId) { return } startTime = Date.now() timerId = setInterval(() => { if (callback) { callback(formatTime()) } }, 1000) } // 停止计时器
function stopPosityveTimer() { if (timerId) { clearInterval(timerId) timerId = null } } // 格式化时间为 HH:MM:SS
function formatTime() { if (!startTime) { return '00:00:00' } const totalSeconds = Math.floor((Date.now() - startTime) / 1000) const hours = Math.floor(totalSeconds / 3600) const minutes = Math.floor((totalSeconds % 3600) / 60) const seconds = totalSeconds % 60 return [ padZero(hours), padZero(minutes), padZero(seconds), ].join(':') }
// 数字补零
function padZero(num: number) { return num < 10 ? `0${num}` : `${num}` }
export { startPosityveTimer, stopPosityveTimer, }
|