App.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import './App.scss';
  2. import React, {useState, useEffect, useRef} from 'react';
  3. import ClockFace from '../src/assets/ClockFace.png';
  4. import ClockFace_M from '../src/assets/ClockFace_M.png';
  5. import ClockFace_H from '../src/assets/ClockFace_H.png';
  6. import ClockFace_S from '../src/assets/ClockFace_S.png';
  7. function Timer ({ms = 10, hour = 0, min = 0, sec = 0} ) {
  8. const [count, setCount] = useState(ms);
  9. const intervalRef = useRef();
  10. const intervalRefHour = useRef();
  11. const intervalRefMin = useRef();
  12. const intervalRefSec = useRef();
  13. const [h, setHour] = useState(hour);
  14. const [m, setMin] = useState(min);
  15. const [s, setSec] = useState(sec);
  16. const [stop, setStop] = useState(true);
  17. const [start, setStart] = useState(true);
  18. useEffect(
  19. () => {
  20. intervalRef.current = setInterval(() => {
  21. setCount((count) => count - 1)
  22. }, 1000);
  23. return () => clearInterval(intervalRef.current);
  24. }
  25. , [start])
  26. useEffect(
  27. () => {
  28. const intervalRefHour = setInterval(() => {
  29. setHour( (h) => +h + 1)
  30. }, 3600000);
  31. return () => clearInterval(intervalRefHour)
  32. }
  33. , []
  34. )
  35. useEffect(
  36. () => {
  37. const intervalRefMin = setInterval(() => {
  38. setMin( (m) => +m + 1)
  39. }, 60000);
  40. return () => clearInterval(intervalRefMin)
  41. }
  42. , []
  43. )
  44. useEffect(
  45. () => {
  46. const intervalRefSec = setInterval(() => {
  47. setSec( (s) => +s + 1)
  48. }, 1000);
  49. return () => clearInterval(intervalRefSec)
  50. }
  51. , []
  52. )
  53. const cancelIntervalButton = () => {
  54. if (stop) {
  55. clearInterval(intervalRef.current);
  56. setStop(!stop);
  57. }
  58. if (!stop) {
  59. setStart(!start);
  60. setStop(!stop);
  61. }
  62. };
  63. useEffect(() => {
  64. if (count < 1) {
  65. cancelIntervalButton(intervalRef.current);
  66. setStop('stop');
  67. }
  68. if (h > 24) {
  69. clearInterval(intervalRefHour);
  70. setHour(0);
  71. }
  72. if (m > 60) {
  73. clearInterval(intervalRefMin);
  74. setMin(0);
  75. }
  76. if (s > 60) {
  77. clearInterval(intervalRefSec);
  78. setSec(0);
  79. }
  80. }, [count, h, m, s]);
  81. return (
  82. <div>
  83. <h3>Timer</h3>
  84. <p>{h}:{m}:{s}</p>
  85. <p>{count}</p>
  86. <button onClick = {cancelIntervalButton}>{stop ? 'Stop' : 'Start'}</button>
  87. </div>
  88. )
  89. }
  90. function Timer1 ({seconds = 10, hour = 0, min = 0, sec = 0} ) {
  91. const [count, setCount] = useState(seconds);
  92. const intervalRef = useRef();
  93. const intervalRefHour = useRef();
  94. const intervalRefMin = useRef();
  95. const intervalRefSec = useRef();
  96. const [h, setHour] = useState(hour);
  97. const [m, setMin] = useState(min);
  98. const [s, setSec] = useState(sec);
  99. const [stop, setStop] = useState(true);
  100. const [start, setStart] = useState(true);
  101. useEffect(
  102. () => {
  103. intervalRef.current = setInterval(() => {
  104. setCount((count) => count - 1)
  105. }, 1000);
  106. return () => clearInterval(intervalRef.current);
  107. }
  108. , [start])
  109. useEffect(
  110. () => {
  111. const intervalRefHour = setInterval(() => {
  112. setHour( (h) => +h + 1)
  113. }, 3600000);
  114. return () => clearInterval(intervalRefHour)
  115. }
  116. , []
  117. )
  118. useEffect(
  119. () => {
  120. const intervalRefMin = setInterval(() => {
  121. setMin( (m) => +m + 1)
  122. }, 60000);
  123. return () => clearInterval(intervalRefMin)
  124. }
  125. , []
  126. )
  127. useEffect(
  128. () => {
  129. const intervalRefSec = setInterval(() => {
  130. setSec( (s) => +s + 1)
  131. }, 1000);
  132. return () => clearInterval(intervalRefSec)
  133. }
  134. , []
  135. )
  136. const cancelIntervalButton = () => {
  137. if (stop) {
  138. clearInterval(intervalRef.current);
  139. setStop(!stop);
  140. }
  141. if (!stop) {
  142. setStart(!start);
  143. setStop(!stop);
  144. }
  145. };
  146. useEffect(() => {
  147. if (count < 1) {
  148. cancelIntervalButton(intervalRef.current);
  149. setStop('stop');
  150. }
  151. if (h > 24) {
  152. clearInterval(intervalRefHour);
  153. setHour(0);
  154. }
  155. if (m > 60) {
  156. clearInterval(intervalRefMin);
  157. setMin(0);
  158. }
  159. if (s > 60) {
  160. clearInterval(intervalRefSec);
  161. setSec(0);
  162. }
  163. }, [count, h, m, s]);
  164. return (
  165. <div>
  166. <p>{h}:{m}:{s}</p>
  167. <p>{count}</p>
  168. <button onClick = {cancelIntervalButton}>{stop ? 'Stop' : 'Start'}</button>
  169. </div>
  170. )
  171. }
  172. function TimerControl () {
  173. const [h, setHour] = useState(0);
  174. const [m, setMin] = useState(0);
  175. const [s, setSec] = useState(0);
  176. const [open, setOpen] = useState(false);
  177. const openTimer = () => {
  178. setOpen(!open)
  179. }
  180. return (
  181. <div className="wrapperTimerControl">
  182. <h3>TimerControl</h3>
  183. <input type="number" min="0" max="59" onChange = {(e) => setHour(e.target.value)}></input>
  184. <input type="number" min="0" max="59" onChange = {(e) => setMin(e.target.value)}></input>
  185. <input type="number" min="0" max="59" onChange = {(e) => setSec(e.target.value)}></input>
  186. <button onClick = {openTimer} >Start</button>
  187. {open && <Timer hour = {h} min = {m} sec = {s}/>}
  188. </div>
  189. )
  190. }
  191. const SecondsTimer = ({seconds}) => <h2>{seconds}</h2>
  192. function TimerContainer ({seconds, refresh, render:S}) {
  193. const [timer, setTimer] = useState(seconds);
  194. const idInterval = useRef();
  195. useEffect( () => {
  196. const startPerf = performance.now();
  197. idInterval.current = setInterval(() => {
  198. setTimer((timer => {
  199. const res = timer - (performance.now() - startPerf);
  200. return res < 1 ? 0 : res;
  201. }))
  202. }
  203. , refresh);
  204. return () => clearInterval(idInterval.current);
  205. }, [timer]);
  206. useEffect(() =>{
  207. if (timer < 1) {
  208. clearInterval(idInterval.current);
  209. }
  210. }, [timer]);
  211. return (
  212. <S seconds = {timer}/>
  213. )
  214. }
  215. function Timer2 ({watch: W} ) {
  216. const time = new Date().toLocaleTimeString().split(':');
  217. const [h, setH] = useState(+time[0]);
  218. const [m, setM] = useState(+time[1]);
  219. const [s, setS] = useState(+time[2]);
  220. useEffect(
  221. () => {
  222. const intervalWatch = setInterval(() => {
  223. const time1 = new Date().toLocaleTimeString().split(':');
  224. setH(+time1[0]);
  225. setM(+time1[1]);
  226. setS(+time1[2]);
  227. }, 1000);
  228. return () => clearInterval(intervalWatch.current)
  229. }
  230. , []
  231. )
  232. return (
  233. <W degH = {30*h} degM = {6*m} degS = {6*s}/>
  234. )
  235. }
  236. function Watch({ degH, degM, degS }) {
  237. return (
  238. <div>
  239. <img src = {ClockFace} alt = "Clock" style = {{position: 'absolute'}}></img>
  240. <img src = {ClockFace_H} alt = "Clock"
  241. style = {
  242. {
  243. position: "absolute",
  244. transform: `rotate(${degH}deg)`,
  245. }
  246. }></img>
  247. <img src = {ClockFace_M} alt = "Clock"
  248. style = {
  249. {
  250. position: 'absolute',
  251. transform: `rotate(${degM}deg)`,
  252. }
  253. }></img>
  254. <img src = {ClockFace_S} alt = "Clock"
  255. style = {
  256. {
  257. position: 'absolute',
  258. transform: `rotate(${degS}deg)`,
  259. }
  260. }></img>
  261. </div>
  262. )
  263. }
  264. function TimerControl1 () {
  265. const [h, setHour] = useState(0);
  266. const [m, setMin] = useState(0);
  267. const [s, setSec] = useState(0);
  268. const [open, setOpen] = useState(false);
  269. const openTimer = () => {
  270. setOpen(!open)
  271. }
  272. return (
  273. <div className="wrapperTimerControl">
  274. <input type="number" min="0" max="59" onChange = {(e) => setHour(e.target.value)}></input>
  275. <input type="number" min="0" max="59" onChange = {(e) => setMin(e.target.value)}></input>
  276. <input type="number" min="0" max="59" onChange = {(e) => setSec(e.target.value)}></input>
  277. <button onClick = {openTimer} >{open ? 'Reset' : 'Start'}</button>
  278. {open && <TimerContainer seconds = {(h*60*60 + m*60 + s) * 1000} refresh = {1000} render={SecondsTimer}/> }
  279. </div>
  280. )
  281. }
  282. function App() {
  283. return (
  284. <div className="App">
  285. <TimerControl/>
  286. <h3>TimerContainer </h3>
  287. <TimerContainer seconds={18000} refresh={1000} render={SecondsTimer}/>
  288. <h3>LCD</h3>
  289. <TimerContainer seconds={18000} refresh={1000} render={Timer1}/>
  290. <h3>Watch</h3>
  291. <Timer2 watch={Watch}/>
  292. <h3 style = {{marginTop: "450px"}}>TimerControl + TimerContainer</h3>
  293. <TimerControl1/>
  294. </div>
  295. );
  296. }
  297. export default App;