@@ -5,7 +5,6 @@ const setButton = document.querySelector("#set");
55const stopButton = document . querySelector ( "#stop" ) ;
66let conuntdownInterval ;
77
8- // time management function
98function formatTime ( totalSeconds ) {
109 const minutes = Math . floor ( totalSeconds / 60 ) ;
1110 const seconds = totalSeconds % 60 ;
@@ -15,49 +14,50 @@ function formatTime(totalSeconds) {
1514 return `Time Remaining: ${ paddedMinutes } :${ paddedSeconds } ` ;
1615}
1716
18- // to clear every change made with conditions
19- function clearEverything ( ) {
17+ function reset ( ) {
18+ clearInterval ( conuntdownInterval ) ;
19+ display . textContent = "Time Remaining: 00:00" ;
2020 display . style . color = "black" ;
2121 setButton . disabled = false ;
2222 document . body . style . background = "" ;
2323}
24-
25- // button event handler
24+ let countdownInterval ;
2625setButton . addEventListener ( "click" , ( ) => {
2726 let timeleft = Number ( totalSeconds . value ) ;
28- setButton . disabled = true ;
2927
30- // time 0
31-
32- if ( timeleft <= 0 ) {
33- alert ( "please enter a number greater than 0!" ) ;
28+ if ( timeleft <= 0 || isNaN ( timeleft ) ) {
29+ alert ( "please enter a number greater than zero (0)!" ) ;
3430 return ;
3531 }
32+ setButton . disabled = true ;
3633
3734 display . textContent = formatTime ( timeleft ) ;
3835
36+ const warningTime = 10 ; // seconds
37+ clearInterval ( conuntdownInterval ) ;
38+
3939 conuntdownInterval = setInterval ( ( ) => {
40- if ( timeleft == 0 ) {
41- clearInterval ( conuntdownInterval ) ;
42- document . body . style . backgroundColor = "grey" ;
43- playAlarm ( ) ;
40+ timeleft -- ;
41+
42+ if ( timeleft >= 0 ) {
43+ display . textContent = formatTime ( timeleft ) ;
4444 }
4545
46- if ( timeleft <= 10 ) {
46+ if ( timeleft <= warningTime ) {
4747 display . style . color = "red" ;
4848 }
4949
50- if ( timeleft >= 0 ) {
51- display . textContent = formatTime ( timeleft ) ;
52- timeleft -- ;
50+ if ( timeleft <= 0 ) {
51+ clearInterval ( conuntdownInterval ) ;
52+ document . body . style . backgroundColor = "grey" ;
53+ playAlarm ( ) ;
5354 }
5455 } , 1000 ) ;
5556} ) ;
5657
57- //stop button clicked
5858stopButton . addEventListener ( "click" , ( ) => {
5959 clearInterval ( conuntdownInterval ) ;
60- clearEverything ( ) ;
60+ reset ( ) ;
6161 pauseAlarm ( ) ;
6262} ) ;
6363
0 commit comments