File tree Expand file tree Collapse file tree 1 file changed +35
-1
lines changed
Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Original file line number Diff line number Diff line change 1- function setAlarm ( ) { }
1+ let timeLeft = 0 ;
2+ let timer = null ;
3+
4+ function formatTime ( seconds ) {
5+ let mins = String ( Math . floor ( seconds / 60 ) ) . padStart ( 2 , "0" ) ;
6+ let secs = String ( seconds % 60 ) . padStart ( 2 , "0" ) ;
7+ return `${ mins } :${ secs } ` ;
8+ }
9+
10+ function setAlarm ( ) {
11+ const input = document . getElementById ( "alarmSet" ) . value ;
12+ const display = document . getElementById ( "timeRemaining" ) ;
13+
14+ timeLeft = parseInt ( input , 10 ) ;
15+
16+ if ( isNaN ( timeLeft ) || timeLeft <= 0 ) {
17+ alert ( "Please enter a valid number of seconds." ) ;
18+ return ;
19+ }
20+
21+ display . textContent = `Time Remaining: ${ formatTime ( timeLeft ) } ` ;
22+
23+ if ( timer ) {
24+ clearInterval ( timer ) ;
25+ }
26+ timer = setInterval ( ( ) => {
27+ if ( timeLeft > 0 ) {
28+ timeLeft -- ;
29+ display . textContent = `Time Remaining: ${ formatTime ( timeLeft ) } ` ;
30+ } else {
31+ clearInterval ( timer ) ;
32+ playAlarm ( ) ;
33+ }
34+ } , 1000 ) ;
35+ }
236
337// DO NOT EDIT BELOW HERE
438
You can’t perform that action at this time.
0 commit comments