Skip to content

Commit c7d5f7d

Browse files
committed
Add alarm flashing effect and improve alarm handling logic
1 parent 584bd1d commit c7d5f7d

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,48 @@ let countdownInterval = null;
22
let secondsLeft = 0;
33
const heading = document.getElementById("timeRemaining");
44

5+
// Move audio to outer scope so stopAndResetAlarm can reach it
6+
var audio = new Audio("alarmsound.mp3");
7+
8+
function stopAndResetAlarm() {
9+
audio.pause();
10+
audio.currentTime = 0;
11+
}
12+
513
function setAlarm() {
6-
// Read the number of seconds from the input field
14+
// Stop any currently ringing alarm before starting a new one
15+
stopAndResetAlarm();
16+
stopFlashing(); // reset background too
17+
718
const input = document.getElementById("alarmSet");
819
const seconds = parseInt(input.value, 10);
9-
10-
// If nothing useful was entered, do nothing
1120
if (isNaN(seconds) || seconds <= 0) {
1221
return;
1322
}
1423

15-
// Use seconds directly
1624
secondsLeft = seconds;
17-
// Stop any existing countdown
25+
1826
if (countdownInterval !== null) {
1927
clearInterval(countdownInterval);
2028
countdownInterval = null;
2129
}
2230

23-
// Show starting time right away
2431
updateTimeDisplay();
2532

26-
// Start the countdown
2733
countdownInterval = setInterval(() => {
2834
secondsLeft = secondsLeft - 1;
29-
3035
updateTimeDisplay();
31-
3236
if (secondsLeft <= 0) {
33-
// Time's up
3437
clearInterval(countdownInterval);
3538
countdownInterval = null;
3639
secondsLeft = 0;
37-
38-
// Makes sure to show exactly 00:00
3940
updateTimeDisplay();
4041
playAlarm();
42+
startFlashing(); // flash when alarm fires
4143
}
4244
}, 1000);
4345
}
4446

45-
// Helper function
4647
function updateTimeDisplay() {
4748
const minutes = Math.floor(secondsLeft / 60);
4849
const seconds = secondsLeft % 60;
@@ -51,15 +52,19 @@ function updateTimeDisplay() {
5152
heading.textContent = `Time Remaining: ${displayMin}:${displaySec}`;
5253
}
5354

54-
// DO NOT EDIT BELOW HERE
55+
function startFlashing() {
56+
document.body.classList.add("flashing");
57+
}
5558

56-
var audio = new Audio("alarmsound.mp3");
59+
function stopFlashing() {
60+
document.body.classList.remove("flashing");
61+
}
5762

63+
// DO NOT EDIT BELOW HERE
5864
function setup() {
5965
document.getElementById("set").addEventListener("click", () => {
6066
setAlarm();
6167
});
62-
6368
document.getElementById("stop").addEventListener("click", () => {
6469
pauseAlarm();
6570
});
@@ -71,6 +76,13 @@ function playAlarm() {
7176

7277
function pauseAlarm() {
7378
audio.pause();
79+
stopFlashing(); // reset background
80+
81+
// Also pause the countdown
82+
if (countdownInterval !== null) {
83+
clearInterval(countdownInterval);
84+
countdownInterval = null;
85+
}
7486
}
7587

7688
window.onload = setup;

Sprint-3/alarmclock/style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@
1313
h1 {
1414
text-align: center;
1515
}
16+
17+
@keyframes flash {
18+
0%,
19+
100% {
20+
background-color: white;
21+
}
22+
50% {
23+
background-color: red;
24+
}
25+
}
26+
27+
body.flashing {
28+
animation: flash 0.6s infinite;
29+
}

0 commit comments

Comments
 (0)