File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,22 +3,49 @@ const input = document.querySelector("#alarmSet");
33const startBtn = document . querySelector ( "#set" ) ;
44const stopBtn = document . querySelector ( "#stop" ) ;
55
6+ let timer = null ;
7+
8+ function renderTime ( totalSeconds ) {
9+ const minutes = Math . floor ( totalSeconds / 60 ) ;
10+ const seconds = totalSeconds % 60 ;
11+ const mm = String ( minutes ) . padStart ( 2 , "0" ) ;
12+ const ss = String ( seconds ) . padStart ( 2 , "0" ) ;
13+ heading . innerText = `Time Remaining: ${ mm } :${ ss } ` ;
14+ }
15+
616function setAlarm ( ) {
7- let totalSeconds = Number ( input . value ) ;
8- let timer = null ;
17+ const rawValue = input . value . trim ( ) ;
18+ if ( rawValue === "" ) {
19+ return ;
20+ }
21+
22+ let totalSeconds = Number ( rawValue ) ;
23+ if ( Number . isNaN ( totalSeconds ) || totalSeconds < 0 ) {
24+ return ;
25+ }
26+
27+ if ( timer !== null ) {
28+ clearInterval ( timer ) ;
29+ }
30+
31+ renderTime ( totalSeconds ) ;
32+
33+ if ( totalSeconds === 0 ) {
34+ playAlarm ( ) ;
35+ timer = null ;
36+ input . value = "" ;
37+ return ;
38+ }
939
1040 timer = setInterval ( ( ) => {
11- const minutes = Math . floor ( totalSeconds / 60 ) ;
12- const seconds = totalSeconds % 60 ;
13- const mm = String ( minutes ) . padStart ( 2 , "0" ) ;
14- const ss = String ( seconds ) . padStart ( 2 , "0" ) ;
15- heading . innerText = `Time Remaining: ${ mm } :${ ss } ` ;
41+ totalSeconds -- ;
42+ renderTime ( totalSeconds ) ;
43+
1644 if ( totalSeconds === 0 ) {
45+ clearInterval ( timer ) ;
1746 timer = null ;
1847 playAlarm ( ) ;
19- return ;
2048 }
21- totalSeconds -- ;
2249 } , 1000 ) ;
2350
2451 input . value = "" ;
You can’t perform that action at this time.
0 commit comments