Skip to content
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
73 changes: 72 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
function setAlarm() {}
let timer;

function updateDisplay(timeRemaining, heading) {
let minutes = Math.floor(timeRemaining / 60);
let seconds = timeRemaining % 60;

heading.innerText =
"Time Remaining: " +
String(minutes).padStart(2, "0") +
":" +
String(seconds).padStart(2, "0");
}

function setAlarm() {
let input = document.getElementById("alarmSet");
let heading = document.getElementById("timeRemaining");

let timeRemaining = parseInt(input.value);

if (isNaN(timeRemaining) || timeRemaining <= 0) {
alert("Please enter a positive number of seconds.");
return;
}

clearInterval(timer);
pauseAlarm();

audio.currentTime = 0;
document.body.style.backgroundColor = "";

updateDisplay(timeRemaining, heading);

function countdown() {
timeRemaining = timeRemaining - 1;

updateDisplay(timeRemaining, heading);

if (timeRemaining <= 0) {
clearInterval(timer);
document.body.style.backgroundColor = "red";
playAlarm();
}
}

timer = setInterval(countdown, 1000);
}

function stopAlarm() {
clearInterval(timer);
pauseAlarm();
}

const setButton = document.getElementById("set");
const stopButton = document.getElementById("stop");

setButton.addEventListener(
"click",
function (event) {
event.stopImmediatePropagation();
setAlarm();
},
true
);
Comment on lines +56 to +63

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.

Why not just let the original code to call setAlarm()?


stopButton.addEventListener(
"click",
function (event) {
event.stopImmediatePropagation();
stopAlarm();
},
true
);
Comment on lines +65 to +72

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.

Given that the original code would call pauseAlarm(), an alternative is to add another event listener that calls only "clearInterval()", and let the original code stop the audio.


// DO NOT EDIT BELOW HERE

Expand Down
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