File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- function setAlarm ( ) { }
1+ function setAlarm ( ) {
2+ const input = document . getElementById ( "alarmSet" ) . value ;
3+
4+ if ( ! input || input <= 0 ) {
5+ alert ( "Please enter a valid number" ) ;
6+ return ;
7+ }
8+
9+ let timeRemaining = parseInt ( input ) ;
10+ const display = document . getElementById ( "timeRemaining" ) ;
11+
12+ // Show initial time
13+ display . textContent = `Time Remaining: 00:${ timeRemaining
14+ . toString ( )
15+ . padStart ( 2 , "0" ) } `;
16+
17+ // Clear any previous timer
18+ clearInterval ( countdown ) ;
19+
20+ countdown = setInterval ( ( ) => {
21+ timeRemaining -- ;
22+
23+ display . textContent = `Time Remaining: 00:${ timeRemaining
24+ . toString ( )
25+ . padStart ( 2 , "0" ) } `;
26+
27+ if ( timeRemaining === 0 ) {
28+ clearInterval ( countdown ) ;
29+ playAlarm ( ) ;
30+ }
31+ } , 1000 ) ;
32+ }
233
334// DO NOT EDIT BELOW HERE
435
36+ let countdown ; // 👈 needed for timer control
37+
538var audio = new Audio ( "alarmsound.mp3" ) ;
639
740function setup ( ) {
@@ -20,6 +53,7 @@ function playAlarm() {
2053
2154function pauseAlarm ( ) {
2255 audio . pause ( ) ;
56+ clearInterval ( countdown ) ;
2357}
2458
25- window . onload = setup ;
59+ window . onload = setup ;
You can’t perform that action at this time.
0 commit comments