@@ -13,6 +13,7 @@ if (typeof Audio === "undefined") {
1313}
1414
1515let countdownIntervalId ; //stores timer ID so we can clear it when needed
16+ let alarmRunning = false ; // tracks if a countdown is currently active
1617
1718const formatTime = ( seconds ) => {
1819 // takes a number of seconds and formats it into a string in the format "MM:SS"
@@ -26,22 +27,39 @@ function setAlarm() {
2627 const alarmInput = document . getElementById ( "alarmSet" ) ; // gets input for seconds
2728 const timerDisplay = document . getElementById ( "timeRemaining" ) ; // gets element showing remaining time
2829
30+ // Prevent starting a new alarm if one is already running
31+ if ( alarmRunning ) {
32+ alert ( "Please press Stop before setting a new alarm." ) ; // ensues user cannot set another alarm without stopping the current one first
33+ return ;
34+ }
2935 const seconds = parseInt ( alarmInput . value , 10 ) ; // converts input value to integer
36+
3037 if ( isNaN ( seconds ) || seconds < 0 ) return ; // if ignore invalid or negative input
3138
3239 let remainingSeconds = seconds ; // initializes countdown
3340
3441 timerDisplay . textContent = `Time Remaining: ${ formatTime ( remainingSeconds ) } ` ; // shows initial time
3542
36- if ( countdownIntervalId ) clearInterval ( countdownIntervalId ) ; // stops previous countdown to prevent multiple timers from running simultaneously
43+ if ( countdownIntervalId ) {
44+ clearInterval ( countdownIntervalId ) ; // stops previous countdown to prevent multiple timers from running simultaneously
45+ countdownIntervalId = null ; // resets timer ID
46+ }
47+
48+ if ( remainingSeconds === 0 ) {
49+ // if the input is zero, play the alarm immediately without starting a countdown
50+ playAlarm ( ) ;
51+ return ;
52+ }
3753
54+ alarmRunning = true ; // mark that alarm is active
3855 countdownIntervalId = setInterval ( ( ) => {
39- // timer runs every second
4056 remainingSeconds -- ; // decrements remaining time by 1 second
4157
4258 if ( remainingSeconds <= 0 ) {
4359 // when time runs out, clear the timer and play the alarm sound
44- clearInterval ( countdownIntervalId ) ; // stops the countdown
60+ clearInterval ( countdownIntervalId ) ; // stops the countdown timer
61+ countdownIntervalId = null ; // resets timer ID
62+ alarmRunning = false ; // mark that alarm is no longer active
4563 timerDisplay . textContent = "Time Remaining: 00:00" ; // shows zero time
4664 playAlarm ( ) ; // plays alarm sound
4765 return ; // prevents further execution
@@ -50,6 +68,26 @@ function setAlarm() {
5068 } , 1000 ) ; // repeats every second
5169}
5270
71+ window . addEventListener ( "load" , ( ) => {
72+ // Add a listener to the Stop button that also clears the timer
73+ const stopBtn = document . getElementById ( "stop" ) ;
74+
75+ stopBtn . addEventListener ( "click" , ( ) => {
76+ // stops countdown amd stops audio
77+ if ( countdownIntervalId ) {
78+ clearInterval ( countdownIntervalId ) ;
79+ countdownIntervalId = null ;
80+ }
81+ alarmRunning = false ; // mark that alarm is no longer active
82+
83+ document . getElementById ( "timeRemaining" ) . textContent =
84+ "Time Remaining: 00:00" ; // resets display when stopped
85+
86+ document . getElementById ( "alarmSet" ) . value = "" ; // clears input field when stopped
87+ pauseAlarm ( ) ; // stop audio if it's playing
88+ } ) ;
89+ } ) ;
90+
5391// DO NOT EDIT BELOW HERE
5492
5593var audio = new Audio ( "alarmsound.mp3" ) ;
0 commit comments