File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed
Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 1- function setAlarm ( ) { }
1+ timerInterval = null ;
2+ function setAlarm ( ) {
3+ // 1. Get the input from the user
4+ const inputField = document . getElementById ( "alarmSet" ) ;
5+ let timeRemaining = parseInt ( inputField . value ) ;
6+
7+ // 2. Validate: If no number or 0 is entered, do nothing
8+ if ( isNaN ( timeRemaining ) || timeRemaining <= 0 ) {
9+ return ;
10+ }
11+
12+ // 3. Clear any existing timer; prevent multiple alarms running at once
13+ clearInterval ( timerInterval ) ;
14+
15+ // 4. Update the display immediately
16+ updateTimeDisplay ( timeRemaining ) ;
17+
18+ // 5. Start the countdown
19+ timerInterval = setInterval ( ( ) => {
20+ timeRemaining -= 1 ;
21+
22+ updateTimeDisplay ( timeRemaining ) ;
23+
24+ // 6. Check if timer hit zero
25+ if ( timeRemaining <= 0 ) {
26+ clearInterval ( timerInterval ) ;
27+ playAlarm ( ) ;
28+ document . body . style . backgroundColor = "red" ;
29+ }
30+ } , 1000 ) ;
31+ }
32+
33+ // mm:ss formatting
34+
35+ //00:00 format
236
337// DO NOT EDIT BELOW HERE
438
Original file line number Diff line number Diff line change 44 < meta charset ="utf-8 " />
55 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
66 < link rel ="stylesheet " href ="style.css " />
7- < title > Title here </ title >
7+ < title > Alarm clock app </ title >
88 </ head >
99 < body >
1010 < div class ="centre ">
You can’t perform that action at this time.
0 commit comments