Skip to content
Closed
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
55 changes: 53 additions & 2 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
function setAlarm() {}
let countdown = null;

function updateDisplay(time) {
const timeDisplay = document.getElementById("timeRemaining");
let minutes = Math.floor(time / 60)
.toString()
.padStart(2, "0");
let seconds = (time % 60).toString().padStart(2, "0");
timeDisplay.innerText = "Time Remaining: " + minutes + ":" + seconds;
}

function resetAlarm() {
if (countdown !== null) {
clearInterval(countdown);
countdown = null;
}

pauseAlarm();
audio.currentTime = 0;
updateDisplay(0);
}

function setAlarm() {
resetAlarm();

let secondsLeft = parseInt(document.getElementById("alarmSet").value, 10);

if (isNaN(secondsLeft) || secondsLeft <= 0) {
updateDisplay(0);
return;
}

updateDisplay(secondsLeft);

countdown = setInterval(() => {
secondsLeft = secondsLeft - 1;
updateDisplay(secondsLeft);

if (secondsLeft <= 0) {
clearInterval(countdown);
countdown = null;
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand All @@ -12,6 +56,13 @@ function setup() {
document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});

document.getElementById("stop").addEventListener("click", () => {
if (countdown !== null) {
clearInterval(countdown);
countdown = null;
}
Comment on lines +61 to +64
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also consider using resetAlarm as the callback, trading slight inefficiency for simplicity (and to keep the logic of resetting the alarm in one place). The code won't break when pauseAlarm() is called twice.

});
}

function playAlarm() {
Expand All @@ -22,4 +73,4 @@ function pauseAlarm() {
audio.pause();
}

window.onload = setup;
window.onload = setup;
2 changes: 1 addition & 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 Down
Loading