1- function setAlarm ( ) { }
1+ // This variable will store the interval so we can stop it later
2+ let flashInterval ;
3+
4+ // This function makes the background flash red and white
5+ function startFlashing ( ) {
6+ let isRed = false ; // keeps track of which color we should show
7+
8+ flashInterval = setInterval ( function ( ) {
9+ if ( isRed ) {
10+ document . body . style . backgroundColor = "white" ;
11+ } else {
12+ document . body . style . backgroundColor = "red" ;
13+ }
14+
15+ // Switch the color for next time
16+ isRed = ! isRed ;
17+ } , 500 ) ; // run every half second
18+ }
19+
20+ // This function stops the flashing and resets the background
21+ function stopFlashing ( ) {
22+ clearInterval ( flashInterval ) ; // stop the flashing interval
23+ document . body . style . backgroundColor = "white" ; // reset background
24+ }
25+
26+ function setAlarm ( ) {
27+ // get the value of the input
28+ const input = document . getElementById ( "alarmSet" ) . value ;
29+
30+ // If nothing was typed, exit the function
31+ if ( ! input ) {
32+ return ;
33+ }
34+
35+ let totalSeconds ;
36+
37+ // If the user typed something like "2:21"
38+ if ( input . includes ( ":" ) ) {
39+ // Split into minutes and seconds
40+ const parts = input . split ( ":" ) ;
41+ const minutes = Number ( parts [ 0 ] ) ;
42+ const seconds = Number ( parts [ 1 ] ) ;
43+
44+ // Convert everything into total seconds
45+ totalSeconds = minutes * 60 + seconds ;
46+ } else {
47+ // Otherwise treat the input as normal seconds
48+ totalSeconds = Number ( input ) ;
49+ }
50+
51+ // This is the countdown number that will go down every second
52+ let timeLeft = totalSeconds ;
53+
54+ // If a previous timer was running, stop it
55+ if ( window . countdownTimer ) {
56+ clearInterval ( window . countdownTimer ) ;
57+ }
58+
59+ // A simple helper to turn seconds into MM:SS format
60+ function formatTime ( seconds ) {
61+ const mins = Math . floor ( seconds / 60 ) ;
62+ const secs = seconds % 60 ;
63+
64+ // Make sure both numbers always have two digits
65+ const paddedMins = String ( mins ) . padStart ( 2 , "0" ) ;
66+ const paddedSecs = String ( secs ) . padStart ( 2 , "0" ) ;
67+
68+ return `${ paddedMins } :${ paddedSecs } ` ;
69+ }
70+
71+ // Show the starting time immediately
72+ document . getElementById ( "timeRemaining" ) . innerText =
73+ `Time Remaining: ${ formatTime ( timeLeft ) } ` ;
74+
75+ // Start the countdown — runs every 1000ms (1 second)
76+ window . countdownTimer = setInterval ( ( ) => {
77+ timeLeft -- ; // reduce by 1 second
78+
79+ // Update the heading each second
80+ document . getElementById ( "timeRemaining" ) . innerText =
81+ `Time Remaining: ${ formatTime ( timeLeft ) } ` ;
82+
83+ // When the timer reaches zero
84+ if ( timeLeft <= 0 ) {
85+ clearInterval ( window . countdownTimer ) ; // stop the countdown
86+ clearInterval ( flashInterval ) ; // stop the flashing if it was running
87+ document . getElementById ( "timeRemaining" ) . innerText =
88+ `Time Remaining: 00:00` ;
89+ playAlarm ( ) ; // play the alarm sound
90+ startFlashing ( ) ; // start flashing the background
91+ }
92+ } , 1000 ) ;
93+ }
294
395// DO NOT EDIT BELOW HERE
496
@@ -11,6 +103,7 @@ function setup() {
11103
12104 document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
13105 pauseAlarm ( ) ;
106+ stopFlashing ( ) ; // stop flashing if running
14107 } ) ;
15108}
16109
0 commit comments