1- function setAlarm ( ) { }
1+ // -------- Alarm Clock Implementation --------
2+ let countdownInterval = null ;
3+ let flashInterval = null ;
4+ let timeLeft = 0 ;
5+ let paused = false ;
6+
7+ function setAlarm ( ) {
8+ const input = document . getElementById ( "alarmSet" ) ;
9+ const heading = document . getElementById ( "timeRemaining" ) ;
10+
11+ const secondsInput = parseInt ( input . value , 10 ) ;
12+
13+ if ( isNaN ( secondsInput ) || secondsInput < 0 ) return ;
14+
15+ // Reset previous timer if any
16+ if ( countdownInterval ) clearInterval ( countdownInterval ) ;
17+ stopFlashing ( ) ;
18+ paused = false ;
19+ timeLeft = secondsInput ;
20+
21+ updateHeading ( timeLeft ) ;
22+
23+ countdownInterval = setInterval ( ( ) => {
24+ if ( ! paused ) {
25+ timeLeft -- ;
26+ if ( timeLeft <= 0 ) {
27+ clearInterval ( countdownInterval ) ;
28+ countdownInterval = null ;
29+ updateHeading ( 0 ) ;
30+ playAlarm ( ) ;
31+ startFlashing ( ) ;
32+ } else {
33+ updateHeading ( timeLeft ) ;
34+ }
35+ }
36+ } , 1000 ) ;
37+ }
38+
39+ function pauseAlarm ( ) {
40+ paused = true ;
41+ if ( audio ) audio . pause ( ) ;
42+ stopFlashing ( ) ;
43+ }
44+
45+ function resumeAlarm ( ) {
46+ if ( timeLeft > 0 ) paused = false ;
47+ }
48+
49+ function stopAlarm ( ) {
50+ paused = true ;
51+ if ( audio ) audio . pause ( ) ;
52+ stopFlashing ( ) ;
53+ clearInterval ( countdownInterval ) ;
54+ countdownInterval = null ;
55+ timeLeft = 0 ;
56+ updateHeading ( 0 ) ;
57+ }
58+
59+ function resetAlarm ( ) {
60+ paused = false ;
61+ clearInterval ( countdownInterval ) ;
62+ countdownInterval = null ;
63+ stopFlashing ( ) ;
64+ timeLeft = 0 ;
65+ updateHeading ( 0 ) ;
66+ }
67+
68+ // -------- Helper Functions --------
69+ function updateHeading ( seconds ) {
70+ const heading = document . getElementById ( "timeRemaining" ) ;
71+ const mins = Math . floor ( seconds / 60 )
72+ . toString ( )
73+ . padStart ( 2 , "0" ) ;
74+ const secs = ( seconds % 60 ) . toString ( ) . padStart ( 2 , "0" ) ;
75+ heading . innerText = `Time Remaining: ${ mins } :${ secs } ` ;
76+ }
77+
78+ // ---------------- Flashing Screen ----------------
79+ function startFlashing ( ) {
80+ const body = document . body ;
81+ let isBlue = false ;
82+ flashInterval = setInterval ( ( ) => {
83+ body . style . backgroundColor = isBlue ? "white" : "#add8e6" ;
84+ isBlue = ! isBlue ;
85+ } , 500 ) ;
86+ }
87+
88+ function stopFlashing ( ) {
89+ clearInterval ( flashInterval ) ;
90+ flashInterval = null ;
91+ document . body . style . backgroundColor = "white" ;
92+ }
293
394// DO NOT EDIT BELOW HERE
495
@@ -9,16 +100,28 @@ function setup() {
9100 setAlarm ( ) ;
10101 } ) ;
11102
12- document . getElementById ( "stop " ) . addEventListener ( "click" , ( ) => {
103+ document . getElementById ( "pause " ) . addEventListener ( "click" , ( ) => {
13104 pauseAlarm ( ) ;
14105 } ) ;
106+
107+ document . getElementById ( "resume" ) . addEventListener ( "click" , ( ) => {
108+ resumeAlarm ( ) ;
109+ } ) ;
110+
111+ document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
112+ stopAlarm ( ) ;
113+ } ) ;
114+
115+ document . getElementById ( "reset" ) . addEventListener ( "click" , ( ) => {
116+ resetAlarm ( ) ;
117+ } ) ;
15118}
16119
17120function playAlarm ( ) {
18121 audio . play ( ) ;
19122}
20123
21- function pauseAlarm ( ) {
124+ function pauseAlarmSound ( ) {
22125 audio . pause ( ) ;
23126}
24127
0 commit comments