Skip to content

London | ITP-May-2025 | Hibo Sharif | Module-Data-Groups | Alarm clock #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
function setAlarm() {}
let timeRemaining = 0;
let timerAlarm = null;
let timerPaused = false;

function setAlarm() {
const input = document.getElementById("alarmSet").value;
timeRemaining = parseInt(input, 10);

if (!timeRemaining || timeRemaining <= 0) {
alert("Please only numbers above 0");
return;
}
timerPaused = false;
updateDisplay();

if (timerAlarm) clearInterval(timerAlarm);

timerAlarm = setInterval(() => {
if (!timerPaused) {
timeRemaining--;
console.log("Time left:", timeRemaining);
updateDisplay();

if (timeRemaining <= 0) {
clearInterval(timerAlarm);
playAlarm();
}
}
}, 1000);
}

function updateDisplay() {
const minutes = String(Math.floor(timeRemaining / 60)).padStart(2, "0");
const seconds = String(timeRemaining % 60).padStart(2, "0");
document.getElementById(
"timeRemaining"
).innerText = `Time Remaining: ${minutes}: ${seconds}`;
}
// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");
Expand All @@ -8,18 +44,24 @@ function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
}
document.getElementById("pause").addEventListener("click", togglePause);

function playAlarm() {
audio.play();
document.body.classList.add("flash");
}

function pauseAlarm() {
audio.pause();
document.body.classList.remove("flash");
}

function togglePause() {
timerPaused = !timerPaused;
}

window.onload = setup;
3 changes: 2 additions & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm clock App</title>
</head>
<body>
<div class="centre">
Expand All @@ -14,6 +14,7 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
<button id="pause" type="button">Pause Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>
Expand Down
9 changes: 9 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@
h1 {
text-align: center;
}
.flash {
animation: flash-bg 1s infinite;
}

@keyframes flash-bg {
0% {background-color: white;}
50%{background-color: rgb(236, 160, 213);}
100%{background-color: white;}
}