消毒机设备
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.

47 lines
997 B

  1. let timerId: ReturnType<typeof setInterval> | null = null
  2. let startTime: number = 0
  3. // 开始计时器
  4. function startPosityveTimer(callback: (timeString: string) => void) {
  5. if (timerId) {
  6. return
  7. }
  8. startTime = Date.now()
  9. timerId = setInterval(() => {
  10. if (callback) {
  11. callback(formatTime())
  12. }
  13. }, 1000)
  14. }
  15. // 停止计时器
  16. function stopPosityveTimer() {
  17. if (timerId) {
  18. clearInterval(timerId)
  19. timerId = null
  20. }
  21. }
  22. // 格式化时间为 HH:MM:SS
  23. function formatTime() {
  24. if (!startTime) {
  25. return '00:00:00'
  26. }
  27. const totalSeconds = Math.floor((Date.now() - startTime) / 1000)
  28. const hours = Math.floor(totalSeconds / 3600)
  29. const minutes = Math.floor((totalSeconds % 3600) / 60)
  30. const seconds = totalSeconds % 60
  31. return [
  32. padZero(hours),
  33. padZero(minutes),
  34. padZero(seconds),
  35. ].join(':')
  36. }
  37. // 数字补零
  38. function padZero(num: number) {
  39. return num < 10 ? `0${num}` : `${num}`
  40. }
  41. export {
  42. startPosityveTimer,
  43. stopPosityveTimer,
  44. }