1
+ function main ( ) {
2
+ const timer = document . querySelector ( '.timer' ) ;
3
+ const startButton = document . querySelector ( '.start' ) ;
4
+ const stopButton = document . querySelector ( '.stop' ) ;
5
+ const restartButton = document . querySelector ( '.restart' ) ;
6
+ let timerJS = [ 0 , 0 , 0 ] ;
7
+ let timerJSString ;
8
+
9
+ function timerFunction ( list ) {
10
+ if ( list [ 2 ] < 59 ) {
11
+ list [ 2 ] ++
12
+ return list
13
+ } else if ( list [ 1 ] < 59 ) {
14
+ list [ 1 ] ++
15
+ return list
16
+ } else if ( list [ 0 ] < 23 ) {
17
+ list [ 0 ] ++
18
+ return list
19
+ } else {
20
+ return list
21
+ }
22
+ }
23
+
24
+
25
+ function convertString ( list ) {
26
+ list = [ `${ list [ 0 ] } ` , `${ list [ 1 ] } ` , `${ list [ 2 ] } ` ]
27
+ if ( list [ 2 ] < 10 ) {
28
+ list [ 2 ] = `0${ list [ 2 ] } `
29
+ }
30
+ if ( list [ 1 ] < 10 ) {
31
+ list [ 1 ] = `0${ list [ 1 ] } `
32
+ }
33
+ if ( list [ 0 ] < 10 ) {
34
+ list [ 0 ] = `0${ list [ 0 ] } `
35
+ }
36
+ return list
37
+ }
38
+
39
+
40
+ startButton . addEventListener ( 'click' , function startFunction ( ) {
41
+ const time = setInterval ( function time ( ) {
42
+ timer . classList . remove ( 'paused' )
43
+ timerJSString = convertString ( timerFunction ( timerJS ) )
44
+ timer . innerHTML = `${ timerJSString [ 0 ] } :${ timerJSString [ 1 ] } :${ timerJSString [ 2 ] } `
45
+ } , 900 ) ;
46
+ stopButton . addEventListener ( 'click' , function ( ) {
47
+ setTimeout ( function ( ) {
48
+ clearInterval ( time )
49
+ } , 10 ) ;
50
+ timer . classList . add ( 'paused' )
51
+ } )
52
+ return
53
+ } ) ;
54
+ restartButton . addEventListener ( 'click' , function ( ) {
55
+ timerJS = [ 0 , 0 , 0 ] ;
56
+ timerJSString = [ '00' , '00' , '00' ]
57
+ timer . innerHTML = `${ timerJSString [ 0 ] } :${ timerJSString [ 1 ] } :${ timerJSString [ 2 ] } `
58
+ timer . classList . remove ( 'paused' )
59
+ } )
60
+ }
61
+
62
+
63
+ main ( )
0 commit comments