@@ -2,17 +2,33 @@ let timeRemaining = 0;
22let intervalId = null ;
33let isPaused = false ;
44
5- function reset ( ) { }
5+ function reset ( ) {
6+ // 1. Stop any active interval timer and clear the ID
7+ clearInterval ( intervalId ) ;
8+ intervalId = null ;
9+
10+ // 2. Reset the state variables back to default
11+ timeRemaining = 0 ;
12+ isPaused = false ;
13+
14+ // 3. Stop visual and audio effects
15+ document . body . classList . remove ( "flash" ) ;
16+ pauseAlarm ( ) ;
17+
18+ // 4. Update the screen display back to 00:00
19+ updateDisplay ( 0 ) ;
20+ }
621
722function setAlarm ( ) {
823 const input = document . getElementById ( "alarmSet" ) ;
924 const value = input . value . trim ( ) ; //trim empty string
1025 //Check if the input is empty
11- if ( value === "" ) {
12- alert ( "Please enter a number of seconds ." ) ;
26+ if ( value === "" ) {
27+ alert ( "Please enter a number of seconds." ) ;
28+ return ;
1329 }
1430
15- const seconds = Number ( input . value ) ;
31+ const seconds = Number ( value ) ;
1632//validate that input is a positive integer and doestn't exceed 1 hour
1733 if (
1834 isNaN ( seconds ) ||
@@ -23,34 +39,31 @@ function setAlarm() {
2339 alert ( "Please enter a valid whole number between 1 and 3600." ) ;
2440 return ;
2541 }
42+ reset ( ) ;
2643
2744 timeRemaining = seconds ;
2845 isPaused = false ;
2946 updateDisplay ( timeRemaining ) ;
30-
31- // Clear any previous countdown
32- if ( intervalId ) {
33- clearInterval ( intervalId ) ;
34- }
35-
3647 // Start a new countdown
3748 startCountdown ( ) ;
3849}
3950
40- function startCountdown ( ) {
51+ function startCountdown ( ) {
52+ if ( intervalId !== null ) {
53+ return ;
54+ }
55+
4156 intervalId = setInterval ( ( ) => {
4257 timeRemaining -- ;
4358 updateDisplay ( timeRemaining ) ;
4459
4560 if ( timeRemaining <= 0 ) {
4661 clearInterval ( intervalId ) ;
4762 intervalId = null ;
48- isPaused = true ;
4963 playAlarm ( ) ;
5064 document . body . classList . add ( "flash" ) ;
5165 }
5266 } , 1000 ) ;
53-
5467}
5568
5669function updateDisplay ( seconds ) {
@@ -66,22 +79,20 @@ function togglePause() {
6679 if ( intervalId === null && ! isPaused ) {
6780 return ;
6881 }
82+
6983 if ( ! isPaused ) {
7084 clearInterval ( intervalId ) ;
85+ intervalId = null ;
7186 isPaused = true ;
7287 } else {
7388 isPaused = false ;
7489 startCountdown ( ) ;
7590 }
7691}
77-
7892document . getElementById ( "pause" ) . addEventListener ( "click" , togglePause ) ;
93+ document . getElementById ( "stop" ) . addEventListener ( "click" , reset ) ;
7994
8095
81- document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
82- document . body . classList . remove ( "flash" ) ;
83- } ) ;
84-
8596
8697// DO NOT EDIT BELOW HERE
8798
0 commit comments