1-
2-
3-
4-
51let timeLeft = 0 ;
62let timer = null ;
73let flashing = null ;
84
95// DOM references
106window . addEventListener ( "DOMContentLoaded" , ( ) => {
7+ const stopButton = document . getElementById ( "stop" ) ;
118
12- const stopButton = document . getElementById ( "stop" ) ;
13-
14- // Event listeners
9+ // Event listeners
1510
16- if ( stopButton ) stopButton . addEventListener ( "click" , stopAlarm ) ;
11+ if ( stopButton ) stopButton . addEventListener ( "click" , stopAlarm ) ;
1712
18- // Show 00:00 on load
19- updateDisplay ( 0 ) ;
13+ // Show 00:00 on load
14+ updateDisplay ( 0 ) ;
2015} ) ;
2116
2217// -------------------------------
@@ -25,21 +20,21 @@ updateDisplay(0);
2520
2621function setAlarm ( ) {
2722 const inputEl = document . getElementById ( "alarmSet" ) ;
28- const input = inputEl . value . trim ( ) ;
23+ const input = inputEl . value . trim ( ) ;
2924
30- // Check empty input
31- if ( input === "" ) {
32- alert ( "Please enter a number of seconds." ) ;
33- return ;
34- }
25+ // Check empty input
26+ if ( input === "" ) {
27+ alert ( "Please enter a number of seconds." ) ;
28+ return ;
29+ }
3530
36- const parsed = parseInt ( input , 10 ) ;
31+ const parsed = parseInt ( input , 10 ) ;
3732
38- // Check invalid or negative number
39- if ( isNaN ( parsed ) || parsed < 0 ) {
40- alert ( "Please enter a valid non-negative number." ) ;
41- return ;
42- }
33+ // Check invalid or negative number
34+ if ( isNaN ( parsed ) || parsed < 0 ) {
35+ alert ( "Please enter a valid non-negative number." ) ;
36+ return ;
37+ }
4338
4439 timeLeft = parsed ;
4540
@@ -48,59 +43,56 @@ if (isNaN(parsed) || parsed < 0) {
4843
4944 // Clear previous countdown
5045 clearInterval ( timer ) ;
51- clearInterval ( flashing ) ; // stop flashing if it was active
46+ clearInterval ( flashing ) ; // stop flashing if it was active
5247 flashing = null ;
5348 document . body . style . backgroundColor = "" ;
54- pauseAlarm ( ) ; // stop any playing audio
49+ pauseAlarm ( ) ; // stop any playing audio
5550 audio . currentTime = 0 ;
5651
5752 function tick ( ) {
58- if ( timeLeft > 0 ) {
59- timeLeft -- ;
60- updateDisplay ( timeLeft ) ;
61- } else {
62- clearInterval ( timer ) ;
63- startAlarm ( ) ;
53+ if ( timeLeft > 0 ) {
54+ timeLeft -- ;
55+ updateDisplay ( timeLeft ) ;
56+ } else {
57+ clearInterval ( timer ) ;
58+ startAlarm ( ) ;
59+ }
6460 }
65- }
6661
67- // Run once immediately for consistency
68- tick ( ) ;
69- timer = setInterval ( tick , 1000 ) ;
62+ // Run once immediately for consistency
63+ tick ( ) ;
64+ timer = setInterval ( tick , 1000 ) ;
7065}
7166
7267function updateDisplay ( seconds ) {
7368 const mins = String ( Math . floor ( seconds / 60 ) ) . padStart ( 2 , "0" ) ;
7469 const secs = String ( seconds % 60 ) . padStart ( 2 , "0" ) ;
75-
70+
7671 const display = document . getElementById ( "timeRemaining" ) ;
77- if ( ! display ) return ;
72+ if ( ! display ) return ;
7873 display . textContent = `Time Remaining: ${ mins } :${ secs } ` ;
7974}
8075
8176function startAlarm ( ) {
8277 playAlarm ( ) ;
8378
8479 // Flashing background
85- if ( ! flashing ) {
80+ if ( ! flashing ) {
8681 flashing = setTimeout ( ( ) => {
87- document . body . style . backgroundColor =
88- document . body . style . backgroundColor === "red" ? "orange" : "red" ;
89- } , 300 ) ;
90- }
82+ document . body . style . backgroundColor =
83+ document . body . style . backgroundColor === "red" ? "orange" : "red" ;
84+ } , 300 ) ;
85+ }
9186}
9287
9388function stopAlarm ( ) {
94-
9589 clearInterval ( flashing ) ;
9690 flashing = null ;
9791 document . body . style . backgroundColor = "" ;
9892}
9993
100-
10194// module.exports= setAlarm;
10295
103-
10496// DO NOT EDIT BELOW HERE
10597
10698var audio = new Audio ( "alarmsound.mp3" ) ;
@@ -121,6 +113,6 @@ function playAlarm() {
121113
122114function pauseAlarm ( ) {
123115 audio . pause ( ) ;
124- }
116+ }
125117
126118window . onload = setup ;
0 commit comments