WorkoutTimer.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { View, Text } from "@tarojs/components";
  2. import './WorkoutStopWatch.scss'
  3. import { useEffect, useState } from "react";
  4. import Box from "@/components/layout/Box";
  5. import { ChooseScenarioBtn } from "../common/SpecBtns";
  6. import { ColorType } from "@/context/themes/color";
  7. var timer
  8. export default function Component() {
  9. const schedules = [{
  10. type: 'group',
  11. time: 5,
  12. index: 1,
  13. }, {
  14. type: 'rest',
  15. time: 5,
  16. }, {
  17. type: 'group',
  18. time: 3,
  19. index: 2
  20. }, {
  21. type: 'rest',
  22. time: 2,
  23. }, {
  24. type: 'group',
  25. time: 5,
  26. index: 3
  27. }, {
  28. type: 'group',
  29. time: 2,
  30. index: 4
  31. }, {
  32. type: 'group',
  33. time: 3,
  34. index: 5
  35. }]
  36. const [current, setCurrent] = useState(0)
  37. const [isDoing, setIsDoing] = useState(false)
  38. const [records, setRecords] = useState<any>([])
  39. const [countdown, setCountdown] = useState('')
  40. const [count, setCount] = useState(0)
  41. useEffect(() => {
  42. if (records.length == 0) {
  43. return
  44. }
  45. var record = records[records.length - 1]
  46. record.count += 1
  47. if (record.count == schedules[current].time) {
  48. if (current == schedules.length - 1) {
  49. clearInterval(timer)
  50. setCountdown('00:00:00')
  51. return
  52. }
  53. setCurrent(current + 1)
  54. records.push({
  55. start: new Date().getTime(),
  56. count: 0
  57. })
  58. setRecords(records)
  59. }
  60. countdownText()
  61. // console.log(count)
  62. // console.log(record)
  63. }, [count])
  64. function start() {
  65. setIsDoing(true)
  66. setRecords([{
  67. start: new Date().getTime(),
  68. count: 0
  69. }])
  70. timer = setInterval(() => {
  71. setCount(count => count + 1)
  72. }, 1000)
  73. }
  74. function pause() {
  75. }
  76. function resmue() {
  77. }
  78. function terminal() {
  79. }
  80. function countdownText() {
  81. var count = 0
  82. var index = current
  83. if (records.length > 0) {
  84. count = records[records.length - 1].count
  85. if (count == 0 && records.length > 1) {
  86. count = records[records.length - 2].count
  87. // index -= 1
  88. }
  89. }
  90. var left = schedules[index].time - count
  91. const hours = Math.floor(left / (60 * 60));
  92. const minutes = Math.floor((left % (60 * 60)) / 60);
  93. const seconds = Math.floor(left % 60);
  94. var str = hours > 10 ? hours : '0' + hours
  95. str += ':'
  96. str += (minutes > 10 ? minutes : '0' + minutes) + ''
  97. str += ':'
  98. str += (seconds > 10 ? seconds : '0' + seconds) + ''
  99. setCountdown(str+'')
  100. // console.log(left,str)
  101. // return str
  102. }
  103. return <View style={{ display: 'flex', flexDirection: 'column', color: '#fff' }}>
  104. <Box>
  105. <View style={{ display: 'flex', flexDirection: 'column' }}>
  106. <Text>第{current + 1}组</Text>
  107. <Text className="working_duration">{countdown}</Text>
  108. {!isDoing && <ChooseScenarioBtn onClick={start} title="开始" background={ColorType.workout} />}
  109. </View>
  110. </Box>
  111. <Text className="working_end" onClick={terminal}>结束训练</Text>
  112. </View>
  113. }