1+ let countdown ;
2+
3+ // reset before starting new countdown
4+ function resetAlarm ( ) {
5+ clearInterval ( countdown ) ;
6+ audio . pause ( ) ;
7+ document . getElementById ( "timeRemaining" ) . textContent = "Time Remaining: 00:00" ;
8+ document . body . classList . toggle ( "alarm-activated" , false ) ;
9+ }
10+
111function setAlarm ( ) {
212 let seconds = parseInt ( document . getElementById ( "alarmSet" ) . value ) ;
3-
4- if ( seconds <= 0 ) {
5- alert ( "The number of seconds must be more than 0 please" ) ;
6- return ;
7- }
8- function updateDisplay ( ) {
13+
14+ if ( ! seconds || seconds < 1 ) {
15+ alert ( "The number of seconds must be more than 0 please" ) ;
16+ return ;
17+ }
18+ function updateDisplay ( ) {
919 const minutes = Math . floor ( seconds / 60 ) ;
1020 const remainingSeconds = seconds % 60 ;
1121 document . getElementById ( "timeRemaining" ) . textContent =
@@ -14,14 +24,20 @@ function setAlarm() {
1424
1525 updateDisplay ( ) ; // update immediately on click
1626
17- const countdown = setInterval ( ( ) => {
27+ // code to reset background
28+ function pauseAlarm ( ) {
29+ audio . pause ( ) ;
30+ document . body . classList . toggle ( "alarm-activated" , false ) ;
31+ }
32+
33+ countdown = setInterval ( ( ) => {
1834 seconds -- ;
1935 updateDisplay ( ) ;
20-
36+
2137 if ( seconds <= 0 ) {
2238 clearInterval ( countdown ) ;
2339 playAlarm ( ) ;
24- document . body . style . backgroundColor = "darkorange" ;
40+ document . body . classList . toggle ( "alarm-activated" , true ) ;
2541 }
2642 } , 1000 ) ;
2743}
@@ -44,9 +60,4 @@ function playAlarm() {
4460 audio . play ( ) ;
4561}
4662
47- function pauseAlarm ( ) {
48- audio . pause ( ) ;
49- document . body . style . backgroundColor = "cornflowerblue" ;
50- }
51-
5263window . onload = setup ;
0 commit comments