Skip to content

Commit 21079b1

Browse files
committed
Implement alarm functionality with timer and screen flashing effects
1 parent e226033 commit 21079b1

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,71 @@
1-
function setAlarm() {}
1+
let timerInterval;
2+
let flashInterval;
3+
4+
function setAlarm() {
5+
const inputElement = document.getElementById("alarmSet");
6+
const headingElement = document.getElementById("timeRemaining");
7+
8+
if (!inputElement.value) {
9+
return;
10+
}
11+
12+
if (timerInterval) {
13+
clearInterval(timerInterval);
14+
}
15+
if (flashInterval) {
16+
clearInterval(flashInterval);
17+
}
18+
19+
let timeInSeconds = Number(inputElement.value);
20+
21+
function updateScreen(secondsRemaining) {
22+
const minutes = Math.floor(secondsRemaining / 60);
23+
24+
const seconds = secondsRemaining % 60;
25+
26+
const formattedMinutes = String(minutes).padStart(2, "0");
27+
const formattedSeconds = String(seconds).padStart(2, "0");
28+
29+
headingElement.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
30+
}
31+
32+
// update when we click set alarm
33+
updateScreen(timeInSeconds);
34+
35+
timerInterval = setInterval(() => {
36+
// Subtract 1 from the time
37+
timeInSeconds = timeInSeconds - 1;
38+
39+
// Update the screen with the new time
40+
updateScreen(timeInSeconds);
41+
42+
// when we hit zero?
43+
if (timeInSeconds <= 0) {
44+
clearInterval(timerInterval);
45+
playAlarm();
46+
flashScreen();
47+
}
48+
}, 1000);
49+
}
50+
51+
function flashScreen() {
52+
let flashCount = 0;
53+
54+
flashInterval = setInterval(() => {
55+
flashCount++;
56+
57+
if (document.body.style.backgroundColor === "red") {
58+
document.body.style.backgroundColor = "white";
59+
} else {
60+
document.body.style.backgroundColor = "red";
61+
}
62+
63+
if (flashCount >= 20) {
64+
clearInterval(flashInterval);
65+
document.body.style.backgroundColor = "white";
66+
}
67+
}, 500);
68+
}
269

370
// DO NOT EDIT BELOW HERE
471

@@ -20,6 +87,8 @@ function playAlarm() {
2087

2188
function pauseAlarm() {
2289
audio.pause();
90+
clearInterval(flashInterval);
91+
document.body.style.backgroundColor = "white";
2392
}
2493

2594
window.onload = setup;

Sprint-3/alarmclock/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313
"bugs": {
1414
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
1515
},
16-
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
16+
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
17+
"devDependencies": {
18+
"@testing-library/jest-dom": "^6.9.1",
19+
"jest-environment-jsdom": "^30.3.0"
20+
}
1721
}

0 commit comments

Comments
 (0)