1- function setAlarm ( ) { }
2-
3- // DO NOT EDIT BELOW HERE
4-
5- var audio = new Audio ( "alarmsound.mp3" ) ;
6-
7- function setup ( ) {
8- document . getElementById ( "set" ) . addEventListener ( "click" , ( ) => {
9- setAlarm ( ) ;
10- } ) ;
11-
12- document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
13- pauseAlarm ( ) ;
14- } ) ;
15- }
16-
171const state = {
18- initialTime : 0 ,
192 remainingTime : 0 ,
203 timerId : null ,
214} ;
225
236function setAlarm ( ) {
7+ if ( state . timerId !== null ) {
8+ clearInterval ( state . timerId ) ;
9+ state . timerId = null ;
10+ }
2411 const timeInput = document . getElementById ( "alarmSet" ) ;
25- state . initialTime = + timeInput . value ;
26- state . remainingTime = state . initialTime ;
27- const formattedTime = formatTime ( state . initialTime ) ;
12+ state . remainingTime = + timeInput . value ;
13+ const formattedTime = formatTime ( state . remainingTime ) ;
2814 timeInput . value = "" ;
2915
3016 const displayedTime = document . getElementById ( "timeRemaining" ) ;
@@ -45,20 +31,31 @@ function formatTime(seconds) {
4531
4632function timer ( ) {
4733 const displayedTime = document . getElementById ( "timeRemaining" ) ;
48-
49- console . log ( state . remainingTime ) ;
34+ state . remainingTime -= 1 ;
35+ const countingDownTime = formatTime ( state . remainingTime ) ;
36+ displayedTime . textContent = `Time Remaining: ${ countingDownTime } ` ;
5037 if ( state . remainingTime === 0 ) {
5138 clearInterval ( state . timerId ) ;
5239 state . timerId = null ;
5340 document . body . style . backgroundColor = "red" ;
5441 playAlarm ( ) ;
55- } else {
56- state . remainingTime -= 1 ;
57- const countingDownTime = formatTime ( state . remainingTime ) ;
58- displayedTime . textContent = `Time Remaining: ${ countingDownTime } ` ;
5942 }
6043}
6144
45+ // DO NOT EDIT BELOW HERE
46+
47+ var audio = new Audio ( "alarmsound.mp3" ) ;
48+
49+ function setup ( ) {
50+ document . getElementById ( "set" ) . addEventListener ( "click" , ( ) => {
51+ setAlarm ( ) ;
52+ } ) ;
53+
54+ document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
55+ pauseAlarm ( ) ;
56+ } ) ;
57+ }
58+
6259function playAlarm ( ) {
6360 audio . play ( ) ;
6461}
0 commit comments