11let timerInterval = null ;
2- let remainingSeconds = 0 ;
32
43// Resets timer state
54function resetTimer ( ) {
65 if ( timerInterval !== null ) {
76 clearInterval ( timerInterval ) ;
87 timerInterval = null ;
98 }
10- remainingSeconds = 0 ;
119}
1210//Starts the alarm counbtdown
1311function setAlarm ( ) {
1412 // Read the minutes value from the alarm input field
1513 const minutesInput = document . getElementById ( "alarmSet" ) ;
1614 const seconds = parseInt ( minutesInput . value , 10 ) ;
15+
1716 // Ignore invalid or non-positive input
1817
1918 if ( isNaN ( seconds ) || seconds <= 0 ) {
@@ -22,28 +21,28 @@ function setAlarm() {
2221 // Reset any existing timer first
2322 resetTimer ( ) ;
2423
25- // Store the input as the remaining seconds to count down
26- remainingSeconds = seconds ;
24+ //local variable (no global sharing)
25+ let remainingSeconds = seconds ;
2726
2827 // Update display immediately
29- updateTimeDisplay ( ) ;
28+ updateTimeDisplay ( remainingSeconds ) ;
3029
3130 // Start countdown
3231 timerInterval = setInterval ( ( ) => {
3332 remainingSeconds -- ;
3433
35- updateTimeDisplay ( ) ;
34+ updateTimeDisplay ( remainingSeconds ) ;
3635
3736 if ( remainingSeconds <= 0 ) {
3837 resetTimer ( ) ;
39- updateTimeDisplay ( ) ;
38+ updateTimeDisplay ( 0 ) ;
4039 playAlarm ( ) ;
4140 }
4241 } , 1000 ) ;
4342}
4443
4544// Converts remainingSeconds into MM:SS format and updates the display
46- function updateTimeDisplay ( ) {
45+ function updateTimeDisplay ( remainingSeconds ) {
4746 const minutes = Math . floor ( remainingSeconds / 60 ) ;
4847 const seconds = remainingSeconds % 60 ;
4948
0 commit comments