-
-
Notifications
You must be signed in to change notification settings - Fork 319
London | 26-ITP-May | Eyob Zeray | Sprint 3 | alarmclock #1316
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,41 @@ | ||
| function setAlarm() {} | ||
| let currentIntervalId = null; | ||
|
|
||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); | ||
| const heading = document.getElementById("timeRemaining"); | ||
|
|
||
| let secondsRemaining = Number(input.value); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the number is 0 or negative?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — I added a check right after reading the input to handle that. If the number's 0, negative, or not a real number at all, it just shows a message asking for a valid number and stops there instead of kicking off a countdown: let secondsRemaining = Number(input.value); if (!secondsRemaining || secondsRemaining <= 0) { |
||
|
|
||
| if (!secondsRemaining || secondsRemaining <= 0) { | ||
| heading.innerText = "Please enter a number of seconds greater than 0"; | ||
| return; | ||
| } | ||
|
|
||
| function updateDisplay() { | ||
| const minutes = Math.floor(secondsRemaining / 60); | ||
| const seconds = secondsRemaining % 60; | ||
| const paddedMinutes = String(minutes).padStart(2, "0"); | ||
| const paddedSeconds = String(seconds).padStart(2, "0"); | ||
| heading.innerText = Time Remaining: ${paddedMinutes}:${paddedSeconds}; | ||
| } | ||
|
Comment on lines
+14
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job putting the code for formatting into an own function. This makes it reusable and easier to change the formatting if needed |
||
|
|
||
| updateDisplay(); | ||
|
|
||
| if (currentIntervalId !== null) { | ||
| clearInterval(currentIntervalId); | ||
| } | ||
|
|
||
| currentIntervalId = setInterval(() => { | ||
| secondsRemaining--; | ||
| updateDisplay(); | ||
|
|
||
| if (secondsRemaining <= 0) { | ||
| clearInterval(currentIntervalId); | ||
| currentIntervalId = null; | ||
| playAlarm(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -23,3 +60,4 @@ function pauseAlarm() { | |
| } | ||
|
|
||
| window.onload = setup; | ||
|
|
||
There was a problem hiding this comment.
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 multiple times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good catch — turns out clicking it multiple times was starting a new countdown each time without stopping the old one, so they’d all run at once and mess with the display. I fixed it by keeping track of the current interval outside the function, and clearing it out before starting a fresh one whenever setAlarm() runs again:
let currentIntervalId = null;
function setAlarm() {
// ...validation, then:
if (currentIntervalId !== null) {
clearInterval(currentIntervalId);
}
currentIntervalId = setInterval(() => {
secondsRemaining--;
updateDisplay();
}, 1000);
}