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
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 @@ -13,6 +13,7 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
<input id="alarmSet" type="number" />

<button id="set" type="button">Set Alarm</button>
<button id="pause" type="button">Pause Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
Expand Down
30 changes: 29 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
function setAlarm() {}
function setAlarm() {
const timer = Number(document.querySelector("#alarmSet").value);
const timeRemainingEl = document.querySelector("#timeRemaining");

let targetTime = new Date().getTime() + timer * 1000;

const doAlarm = setInterval( () => {
const timeThisSecond = new Date().getTime();
const timeDifference = targetTime - timeThisSecond;
const secondsLeft = Math.max(0, Math.ceil(timeDifference / 1000));

timeRemainingEl.innerHTML = `Time Remaining: ${formatTimeMMSS(secondsLeft)}`;

if (secondsLeft === 0) {
playAlarm();
clearInterval(doAlarm);
}
}, 1000);
}

function formatTimeMMSS (seconds) {
const minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
return `${String(minutes).padStart(2,"0")}:${String(seconds).padStart(2,"0")}`;
}
// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");

function setup() {
document.getElementById("pause").addEventListener("click", () => {
pauseAlarm();
clearInterval(doAlarm);
});

Comment on lines +31 to +35
Copy link

Choose a reason for hiding this comment

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

Not quite. We're not supposed to edit the code in this section. Please check again.

Copy link
Author

Choose a reason for hiding this comment

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

Done. I changed its place.

document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
Expand Down