1- function setAlarm ( ) { }
1+ let timerInterval ;
2+ let flashInterval ;
3+
4+ function setAlarm ( ) {
5+ const inputElement = document . getElementById ( "alarmSet" ) ;
6+ const headingElement = document . getElementById ( "timeRemaining" ) ;
7+
8+ if ( ! inputElement . value ) {
9+ return ;
10+ }
11+
12+ if ( timerInterval ) {
13+ clearInterval ( timerInterval ) ;
14+ }
15+ if ( flashInterval ) {
16+ clearInterval ( flashInterval ) ;
17+ }
18+
19+ let timeInSeconds = Number ( inputElement . value ) ;
20+
21+ function updateScreen ( secondsRemaining ) {
22+ const minutes = Math . floor ( secondsRemaining / 60 ) ;
23+
24+ const seconds = secondsRemaining % 60 ;
25+
26+ const formattedMinutes = String ( minutes ) . padStart ( 2 , "0" ) ;
27+ const formattedSeconds = String ( seconds ) . padStart ( 2 , "0" ) ;
28+
29+ headingElement . innerText = `Time Remaining: ${ formattedMinutes } :${ formattedSeconds } ` ;
30+ }
31+
32+ // update when we click set alarm
33+ updateScreen ( timeInSeconds ) ;
34+
35+ timerInterval = setInterval ( ( ) => {
36+ // Subtract 1 from the time
37+ timeInSeconds = timeInSeconds - 1 ;
38+
39+ // Update the screen with the new time
40+ updateScreen ( timeInSeconds ) ;
41+
42+ // when we hit zero?
43+ if ( timeInSeconds <= 0 ) {
44+ clearInterval ( timerInterval ) ;
45+ playAlarm ( ) ;
46+ flashScreen ( ) ;
47+ }
48+ } , 1000 ) ;
49+ }
50+
51+ function flashScreen ( ) {
52+ let flashCount = 0 ;
53+
54+ flashInterval = setInterval ( ( ) => {
55+ flashCount ++ ;
56+
57+ if ( document . body . style . backgroundColor === "red" ) {
58+ document . body . style . backgroundColor = "white" ;
59+ } else {
60+ document . body . style . backgroundColor = "red" ;
61+ }
62+
63+ if ( flashCount >= 20 ) {
64+ clearInterval ( flashInterval ) ;
65+ document . body . style . backgroundColor = "white" ;
66+ }
67+ } , 500 ) ;
68+ }
269
370// DO NOT EDIT BELOW HERE
471
@@ -20,6 +87,8 @@ function playAlarm() {
2087
2188function pauseAlarm ( ) {
2289 audio . pause ( ) ;
90+ clearInterval ( flashInterval ) ;
91+ document . body . style . backgroundColor = "white" ;
2392}
2493
2594window . onload = setup ;
0 commit comments