let timerId: ReturnType | 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, }