@@ -2,47 +2,48 @@ let countdownInterval = null;
22let secondsLeft = 0 ;
33const heading = document . getElementById ( "timeRemaining" ) ;
44
5+ // Move audio to outer scope so stopAndResetAlarm can reach it
6+ var audio = new Audio ( "alarmsound.mp3" ) ;
7+
8+ function stopAndResetAlarm ( ) {
9+ audio . pause ( ) ;
10+ audio . currentTime = 0 ;
11+ }
12+
513function setAlarm ( ) {
6- // Read the number of seconds from the input field
14+ // Stop any currently ringing alarm before starting a new one
15+ stopAndResetAlarm ( ) ;
16+ stopFlashing ( ) ; // reset background too
17+
718 const input = document . getElementById ( "alarmSet" ) ;
819 const seconds = parseInt ( input . value , 10 ) ;
9-
10- // If nothing useful was entered, do nothing
1120 if ( isNaN ( seconds ) || seconds <= 0 ) {
1221 return ;
1322 }
1423
15- // Use seconds directly
1624 secondsLeft = seconds ;
17- // Stop any existing countdown
25+
1826 if ( countdownInterval !== null ) {
1927 clearInterval ( countdownInterval ) ;
2028 countdownInterval = null ;
2129 }
2230
23- // Show starting time right away
2431 updateTimeDisplay ( ) ;
2532
26- // Start the countdown
2733 countdownInterval = setInterval ( ( ) => {
2834 secondsLeft = secondsLeft - 1 ;
29-
3035 updateTimeDisplay ( ) ;
31-
3236 if ( secondsLeft <= 0 ) {
33- // Time's up
3437 clearInterval ( countdownInterval ) ;
3538 countdownInterval = null ;
3639 secondsLeft = 0 ;
37-
38- // Makes sure to show exactly 00:00
3940 updateTimeDisplay ( ) ;
4041 playAlarm ( ) ;
42+ startFlashing ( ) ; // flash when alarm fires
4143 }
4244 } , 1000 ) ;
4345}
4446
45- // Helper function
4647function updateTimeDisplay ( ) {
4748 const minutes = Math . floor ( secondsLeft / 60 ) ;
4849 const seconds = secondsLeft % 60 ;
@@ -51,15 +52,19 @@ function updateTimeDisplay() {
5152 heading . textContent = `Time Remaining: ${ displayMin } :${ displaySec } ` ;
5253}
5354
54- // DO NOT EDIT BELOW HERE
55+ function startFlashing ( ) {
56+ document . body . classList . add ( "flashing" ) ;
57+ }
5558
56- var audio = new Audio ( "alarmsound.mp3" ) ;
59+ function stopFlashing ( ) {
60+ document . body . classList . remove ( "flashing" ) ;
61+ }
5762
63+ // DO NOT EDIT BELOW HERE
5864function setup ( ) {
5965 document . getElementById ( "set" ) . addEventListener ( "click" , ( ) => {
6066 setAlarm ( ) ;
6167 } ) ;
62-
6368 document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
6469 pauseAlarm ( ) ;
6570 } ) ;
@@ -71,6 +76,13 @@ function playAlarm() {
7176
7277function pauseAlarm ( ) {
7378 audio . pause ( ) ;
79+ stopFlashing ( ) ; // reset background
80+
81+ // Also pause the countdown
82+ if ( countdownInterval !== null ) {
83+ clearInterval ( countdownInterval ) ;
84+ countdownInterval = null ;
85+ }
7486}
7587
7688window . onload = setup ;
0 commit comments