File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77// When the `Time Remaining` reaches `00:00` the alarm should play a sound. You can make the sound happen by using `playAlarm()`.
88
99// You can stop the alarm sound by pressing the `Stop Alarm` button.
10-
11- //
1210const alarmInput = document . querySelector ( "#alarmSet" ) ;
13- const setButton = document . querySelector ( "#set" ) ;
1411const timeRemaining = document . querySelector ( "#timeRemaining" ) ;
12+
13+ let timer ;
14+
1515function setAlarm ( ) {
16+ clearInterval ( timer ) ;
17+ pauseAlarm ( ) ;
1618 let totalSeconds = Number ( alarmInput . value ) ;
17- const timer = setInterval ( ( ) => {
18- totalSeconds -- ;
19+ alarmInput . value = "" ;
20+ if ( totalSeconds <= 0 || ! Number . isFinite ( totalSeconds ) ) {
21+ timeRemaining . innerText = "Time Remaining: 00:00" ;
22+ return ;
23+ }
24+ function updateTime ( ) {
1925 const minutes = String ( Math . floor ( totalSeconds / 60 ) ) . padStart ( 2 , "0" ) ;
20- const seconds = String ( Math . floor ( totalSeconds % 60 ) ) . padStart ( 2 , "0" ) ;
21- const remainingTime = minutes + ":" + seconds ;
22- timeRemaining . innerText = `Time Remaining:${ remainingTime } ` ;
26+ const seconds = String ( totalSeconds % 60 ) . padStart ( 2 , "0" ) ;
27+
28+ timeRemaining . innerText = `Time Remaining: ${ minutes } :${ seconds } ` ;
29+ }
30+ updateTime ( ) ;
31+ timer = setInterval ( ( ) => {
32+ totalSeconds -- ;
33+ updateTime ( ) ;
34+
2335 if ( totalSeconds === 0 ) {
2436 clearInterval ( timer ) ;
2537 playAlarm ( ) ;
2638 }
2739 } , 1000 ) ;
2840}
29-
30- setButton . addEventListener ( "click" , setAlarm ) ;
3141// DO NOT EDIT BELOW HERE
3242
3343var audio = new Audio ( "alarmsound.mp3" ) ;
You can’t perform that action at this time.
0 commit comments