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
52 changes: 51 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
function setAlarm() {}
function time_convert(num) {
let minutes = Math.floor((num % 3600) / 60);
let seconds = num % 60;
return minutes.toString().padStart(2, '0') + ":" + seconds.toString().padStart(2, '0');
}
let currentInterval = null;

function setup() {
document.getElementById("set").addEventListener("click", setAlarm);
document.getElementById("stop").addEventListener("click", pauseAlarm);
}

function setAlarm() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What happens when a user clicks the set alarm button while the countdown is already running?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the feedback. Function has now been updated to stop multiple timers

if (currentInterval !== null) return;

const alarmSetInputEl = document.getElementById("alarmSet");
let timeEl = Number(alarmSetInputEl.value);
if (!Number.isFinite(timeEl) || timeEl <= 0) {
alert("Please enter a valid number greater than 0");
return;
}
const timeRemainingCounterEl = document.getElementById("timeRemaining");
const button = document.getElementById("set");

button.disabled = true;

currentInterval = setInterval(() => {
timeEl--;

timeRemainingCounterEl.textContent = `Time Remaining: ${time_convert(timeEl)}`;

if (timeEl <= 0) {
clearInterval(currentInterval);
currentInterval = null;

timeRemainingCounterEl.textContent = "Done!";
playAlarm();
button.disabled = false;
}
}, 1000);
}

function pauseAlarm() {
if (currentInterval !== null) {
clearInterval(currentInterval);
currentInterval = null;

document.getElementById("set").disabled = false;
}
}


// DO NOT EDIT BELOW HERE

Expand Down
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 @@ -18,3 +18,4 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
<script src="alarmclock.js"></script>
</body>
</html>

Loading