File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ let milliseconds = 0 ;
2+ let seconds = 0 ;
3+ let minutes = 0 ;
4+ let timer ;
5+
6+ const startButton = document . getElementById ( "start" ) ;
7+ const stopButton = document . getElementById ( "stop" ) ;
8+ const resetButton = document . getElementById ( "reset" ) ;
9+
10+ const updateTime = ( ) => {
11+ milliseconds += 1 ;
12+ if ( milliseconds === 100 ) {
13+ milliseconds = 0 ;
14+ seconds += 1 ;
15+ }
16+ if ( seconds === 60 ) {
17+ seconds = 0 ;
18+ minutes += 1 ;
19+ }
20+ document . getElementById ( "milliseconds" ) . innerText =
21+ milliseconds < 10 ? `0${ milliseconds } ` : milliseconds ;
22+ document . getElementById ( "seconds" ) . innerText =
23+ seconds < 10 ? `0${ seconds } ` : seconds ;
24+ document . getElementById ( "minutes" ) . innerText =
25+ minutes < 10 ? `0${ minutes } ` : minutes ;
26+ } ;
27+
28+ const startTimer = ( ) => {
29+ clearInterval ( timer ) ;
30+ timer = setInterval ( updateTime , 10 ) ;
31+ } ;
32+
33+ const stopTimer = ( ) => {
34+ clearInterval ( timer ) ;
35+ } ;
36+
37+ const resetTimer = ( ) => {
38+ clearInterval ( timer ) ;
39+ milliseconds = 0 ;
40+ seconds = 0 ;
41+ minutes = 0 ;
42+ document . getElementById ( "milliseconds" ) . innerText = "00" ;
43+ document . getElementById ( "seconds" ) . innerText = "00" ;
44+ document . getElementById ( "minutes" ) . innerText = "00" ;
45+ } ;
46+
47+ startButton . addEventListener ( "click" , startTimer ) ;
48+ stopButton . addEventListener ( "click" , stopTimer ) ;
49+ resetButton . addEventListener ( "click" , resetTimer ) ;
You can’t perform that action at this time.
0 commit comments