@@ -14,6 +14,7 @@ if (typeof Audio === "undefined") {
1414
1515let countdownIntervalId ; //stores timer ID so we can clear it when needed
1616let alarmRunning = false ; // tracks if a countdown is currently active
17+ let alarmRinging = false ; // tracks if the alarm sound is currently playing
1718
1819const formatTime = ( seconds ) => {
1920 // takes a number of seconds and formats it into a string in the format "MM:SS"
@@ -22,6 +23,21 @@ const formatTime = (seconds) => {
2223 return `${ String ( minutes ) . padStart ( 2 , "0" ) } :${ String ( remainingSeconds ) . padStart ( 2 , "0" ) } ` ; // formats as MM:SS
2324} ;
2425
26+ function updateTimerDisplay ( timerElement , seconds ) {
27+ timerElement . textContent = `Time Remaining: ${ formatTime ( seconds ) } ` ;
28+ }
29+
30+ function resetAllStates ( timerDisplay ) {
31+ if ( countdownIntervalId ) {
32+ clearInterval ( countdownIntervalId ) ;
33+ countdownIntervalId = null ;
34+ }
35+ alarmRunning = false ;
36+ alarmRinging = false ;
37+ pauseAlarm ( ) ;
38+ updateTimerDisplay ( timerDisplay , 0 ) ;
39+ document . getElementById ( "alarmSet" ) . value = "" ;
40+ }
2541function setAlarm ( ) {
2642 // called "Set" button is clicked
2743 const alarmInput = document . getElementById ( "alarmSet" ) ; // gets input for seconds
@@ -32,13 +48,17 @@ function setAlarm() {
3248 alert ( "Please press Stop before setting a new alarm." ) ; // ensues user cannot set another alarm without stopping the current one first
3349 return ;
3450 }
51+ if ( alarmRinging ) {
52+ resetAllStates ( timerDisplay ) ; // stop previous alarm and reset states
53+ }
54+
3555 const seconds = parseInt ( alarmInput . value , 10 ) ; // converts input value to integer
3656
37- if ( isNaN ( seconds ) || seconds < 0 ) return ; // if ignore invalid or negative input
57+ if ( isNaN ( seconds ) || seconds < 0 ) return ; // ignore invalid or negative input
3858
3959 let remainingSeconds = seconds ; // initializes countdown
4060
41- timerDisplay . textContent = `Time Remaining: ${ formatTime ( remainingSeconds ) } ` ; // shows initial time
61+ updateTimerDisplay ( timerDisplay , remainingSeconds ) ; // shows initial time
4262
4363 if ( countdownIntervalId ) {
4464 clearInterval ( countdownIntervalId ) ; // stops previous countdown to prevent multiple timers from running simultaneously
@@ -47,6 +67,7 @@ function setAlarm() {
4767
4868 if ( remainingSeconds === 0 ) {
4969 // if the input is zero, play the alarm immediately without starting a countdown
70+ alarmRinging = true ; // mark that alarm sound is currently playing
5071 playAlarm ( ) ;
5172 return ;
5273 }
@@ -59,12 +80,16 @@ function setAlarm() {
5980 // when time runs out, clear the timer and play the alarm sound
6081 clearInterval ( countdownIntervalId ) ; // stops the countdown timer
6182 countdownIntervalId = null ; // resets timer ID
83+
6284 alarmRunning = false ; // mark that alarm is no longer active
63- timerDisplay . textContent = "Time Remaining: 00:00" ; // shows zero time
85+ alarmRinging = true ; // mark that alarm sound is currently playing
86+
87+ updateTimerDisplay ( timerDisplay , 0 ) ; // shows zero time
6488 playAlarm ( ) ; // plays alarm sound
6589 return ; // prevents further execution
6690 }
67- timerDisplay . textContent = `Time Remaining: ${ formatTime ( remainingSeconds ) } ` ; // updates countdown display
91+
92+ updateTimerDisplay ( timerDisplay , remainingSeconds ) ; // updates countdown display
6893 } , 1000 ) ; // repeats every second
6994}
7095
@@ -79,12 +104,13 @@ window.addEventListener("load", () => {
79104 countdownIntervalId = null ;
80105 }
81106 alarmRunning = false ; // mark that alarm is no longer active
107+ alarmRinging = false ; // mark that alarm sound is no longer playing
108+ pauseAlarm ( ) ; // stop audio if it's playing
82109
83- document . getElementById ( "timeRemaining" ) . textContent =
84- "Time Remaining: 00:00" ; // resets display when stopped
110+ const timerDisplay = document . getElementById ( "timeRemaining" ) ;
111+ updateTimerDisplay ( timerDisplay , 0 ) ;
85112
86113 document . getElementById ( "alarmSet" ) . value = "" ; // clears input field when stopped
87- pauseAlarm ( ) ; // stop audio if it's playing
88114 } ) ;
89115} ) ;
90116
0 commit comments