1+ let timer ;
2+
13function setAlarm ( ) {
2- timeRemaining = document . getElementById ( "timeRemaining" )
34 timeSet = document . getElementById ( "alarmSet" ) . value
4- const time = convertTime ( timeSet )
5-
6-
7-
8-
9- timeRemaining . textContent = `Time Remaining: ${ remainingMinutes } :${ remainingSeconds } `
5+ const time = countdown ( timeSet )
6+ if ( time > 0 ) {
7+ countdown ( time ) ;
8+ }
109}
1110
1211function countdown ( time ) {
13- setInterval ( ( ) => {
14-
12+ clearInterval ( timer ) ;
13+ let timeRemaining = time ;
14+ if ( timeRemaining > 5999 ) { timeRemaining = 5999 } ;
15+ UpdateShownTime ( timeRemaining )
16+
17+ timer = setInterval ( ( ) => {
18+ timeRemaining -= 1 ;
19+ if ( timeRemaining == 0 ) { clearInterval ( timer ) ; playAlarm ( ) ; }
20+ UpdateShownTime ( timeRemaining )
1521 } , 1000 )
1622}
1723
24+ function UpdateShownTime ( timeRemaining ) {
25+ let timeRemainingOutput = document . getElementById ( "timeRemaining" ) ;
26+ let title = document . getElementById ( "title" ) ;
27+ const formattedTime = convertTime ( timeRemaining ) ;
28+ const printedTime = printTime ( formattedTime ) ;
29+ timeRemainingOutput . textContent = `Time Remaining: ${ printedTime } ` ;
30+ title . textContent = `Time Remaining: ${ printedTime } ` ;
31+ }
32+
1833function convertTime ( timeSeconds ) {
19- if ( timeSeconds > 5999 ) { return [ 99 , 59 ] } ;
2034 const seconds = timeSeconds % 60 ;
2135 const minutes = ( timeSeconds - seconds ) / 60 ;
2236 console . log ( [ minutes , seconds ] )
@@ -29,16 +43,7 @@ function printTime(time) {
2943 return paddedHours + ":" + paddedSeconds ;
3044}
3145
32- printTime ( [ 50 , 40 ] )
33- printTime ( [ 50 , 4 ] )
34- printTime ( [ 5 , 40 ] )
35-
36- convertTime ( 5000000 )
37- convertTime ( 50 )
38- convertTime ( 500 )
39- convertTime ( 5000000 )
4046
41- /*
4247// DO NOT EDIT BELOW HERE
4348
4449var audio = new Audio ( "alarmsound.mp3" ) ;
@@ -62,4 +67,3 @@ function pauseAlarm() {
6267}
6368
6469window . onload = setup ;
65- */
0 commit comments