File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,25 +7,35 @@ function resetAlarm() {
77 updateHeading ( 0 ) ;
88}
99
10+ function showError ( message ) {
11+ const heading = document . getElementById ( "timeRemaining" ) ;
12+ heading . innerText = message ;
13+ }
14+
1015function setAlarm ( ) {
1116 const input = document . getElementById ( "alarmSet" ) ;
1217 let time = Number ( input . value ) ;
1318
19+ // Always reset first so old countdown stops
20+ resetAlarm ( ) ;
21+
22+ // Validate input (reject decimals, negatives, NaN)
1423 if ( isNaN ( time ) || time < 0 || ! Number . isInteger ( time ) ) {
15- updateHeading ( 0 ) ;
24+ showError ( "Please enter a whole number of seconds." ) ;
1625 return ;
1726 }
1827
19- resetAlarm ( ) ;
20-
28+ // If time is 0, trigger alarm immediately
2129 if ( time === 0 ) {
2230 updateHeading ( 0 ) ;
2331 playAlarm ( ) ;
2432 return ;
2533 }
2634
35+ // Show starting time
2736 updateHeading ( time ) ;
2837
38+ // Start countdown
2939 countdown = setInterval ( ( ) => {
3040 time -- ;
3141
You can’t perform that action at this time.
0 commit comments