1+ /*
12function setAlarm() {}
23
34// DO NOT EDIT BELOW HERE
@@ -23,3 +24,135 @@ function pauseAlarm() {
2324}
2425
2526window.onload = setup;
27+ */
28+
29+
30+ let countdownInterval = null ;
31+ let flashInterval = null ;
32+ let timeRemaining = 0 ;
33+ let isPaused = false ;
34+
35+ function formatTime ( totalSeconds ) {
36+ const minutes = Math . floor ( totalSeconds / 60 ) ;
37+ const seconds = totalSeconds % 60 ;
38+
39+ const padMinutes = String ( minutes ) . padStart ( 2 , "0" ) ;
40+ const padSeconds = String ( seconds ) . padStart ( 2 , "0" ) ;
41+
42+ return `${ padMinutes } :${ padSeconds } ` ;
43+ }
44+
45+ function updateDisplay ( ) {
46+ const timeRemainingElement = document . getElementById ( "timeRemaining" ) ;
47+ if ( timeRemainingElement ) {
48+ timeRemainingElement . innerText = `Time Remaining: ${ formatTime ( timeRemaining ) } ` ;
49+ }
50+ }
51+
52+ function startFlashingBackground ( ) {
53+ let isRed = false ;
54+ flashInterval = setInterval ( ( ) => {
55+ document . body . style . backgroundColor = isRed ? "#ffffff" : "#ff4d4d" ;
56+ isRed = ! isRed ;
57+ } , 500 ) ;
58+ }
59+
60+ function stopFlashingBackground ( ) {
61+ if ( flashInterval ) {
62+ clearInterval ( flashInterval ) ;
63+ flashInterval = null ;
64+ }
65+ document . body . style . backgroundColor = "" ;
66+ }
67+
68+ function setAlarm ( ) {
69+ const alarmInput = document . getElementById ( "alarmSet" ) ;
70+
71+ // If currently paused, resume countdown
72+ if ( isPaused ) {
73+ isPaused = false ;
74+ startCountdown ( ) ;
75+ return ;
76+ }
77+
78+ // If an active alarm is already running, toggle pause state
79+ if ( countdownInterval !== null ) {
80+ isPaused = true ;
81+ clearInterval ( countdownInterval ) ;
82+ countdownInterval = null ;
83+ return ;
84+ }
85+
86+ // Read time from input field
87+ const inputSeconds = parseInt ( alarmInput . value , 10 ) ;
88+ if ( isNaN ( inputSeconds ) || inputSeconds <= 0 ) {
89+ alert ( "Please enter a valid number of seconds." ) ;
90+ return ;
91+ }
92+
93+ timeRemaining = inputSeconds ;
94+ stopFlashingBackground ( ) ;
95+ updateDisplay ( ) ;
96+ startCountdown ( ) ;
97+ }
98+
99+ function startCountdown ( ) {
100+ // Clear any existing timer before starting a new one
101+ if ( countdownInterval ) {
102+ clearInterval ( countdownInterval ) ;
103+ }
104+
105+ countdownInterval = setInterval ( ( ) => {
106+ timeRemaining -= 1 ;
107+ updateDisplay ( ) ;
108+
109+ if ( timeRemaining <= 0 ) {
110+ clearInterval ( countdownInterval ) ;
111+ countdownInterval = null ;
112+ playAlarm ( ) ;
113+ startFlashingBackground ( ) ;
114+ }
115+ } , 1000 ) ;
116+ }
117+
118+ // Override pauseAlarm to also clear screen effects and reset timers
119+ const originalPauseAlarm = pauseAlarm ;
120+ pauseAlarm = function ( ) {
121+ if ( typeof originalPauseAlarm === "function" ) {
122+ originalPauseAlarm ( ) ;
123+ } else {
124+ audio . pause ( ) ;
125+ }
126+
127+ // Reset state
128+ if ( countdownInterval ) {
129+ clearInterval ( countdownInterval ) ;
130+ countdownInterval = null ;
131+ }
132+ isPaused = false ;
133+ stopFlashingBackground ( ) ;
134+ } ;
135+
136+ // DO NOT EDIT BELOW HERE
137+
138+ var audio = new Audio ( "alarmsound.mp3" ) ;
139+
140+ function setup ( ) {
141+ document . getElementById ( "set" ) . addEventListener ( "click" , ( ) => {
142+ setAlarm ( ) ;
143+ } ) ;
144+
145+ document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
146+ pauseAlarm ( ) ;
147+ } ) ;
148+ }
149+
150+ function playAlarm ( ) {
151+ audio . play ( ) ;
152+ }
153+
154+ function pauseAlarm ( ) {
155+ audio . pause ( ) ;
156+ }
157+
158+ window . onload = setup ;
0 commit comments