London|26-ITP-January|Alexandru Pocovnicu|Sprint 3| alarm clock#1057
London|26-ITP-January|Alexandru Pocovnicu|Sprint 3| alarm clock#1057alexandru-pocovnicu wants to merge 30 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Code works fine if a user only clicks the "Set Alarm" button once.
However, if the user enters a time and then clicks the "Set Alarm" button multiple times, the countdown clock will not display properly.
Can you fix the issue?
Currently when starting a new countdown, the application does not always return to a clean initial state, which can lead to inconsistent behaviour between runs.
Consider introducing a dedicated reset function to return the app to a clean initial state to help ensure consistency.
|
when i press stopalarm i want the countdown to go to 0,call pauseAlarm(), and the class to be removed from the body,but on line 48 i have this message // DO NOT EDIT BELOW HERE, I can't really think of a way of doing it without adding code bellow that |
cjyuan
left a comment
There was a problem hiding this comment.
Note: To reset countdown display when the user clicks "Stop", and without modifying existing code, you can add another event listener to the stop button:
document.getElementById("stop").addEventListener("click", () => {
// Your code here ...
});
|
|
||
| function updateCountDown() { | ||
| remainingSeconds = totalSeconds; | ||
| updateDisplayedTime(remainingSeconds); | ||
|
|
||
| if (totalSeconds <= 0) { | ||
| finishCountDown(); | ||
| return; | ||
| } | ||
|
|
||
| totalSeconds -= 1; | ||
| } | ||
|
|
||
| updateCountDown(); | ||
| intervalId = setInterval(updateCountDown, 1000); |
There was a problem hiding this comment.
-
Move
remainingSeconds = totalSeconds;out ofupdateCountDown()and replacetotalSecondsin the function byremainingSecondsso that the function depends only onremainingSeconds. -
Move
updateCountDown()to outer scope so that it can be used bypauseCountDown().
That is,
function updateCountDown() {
...
}
function setAlarm() {
// Code to deal with input
...
remainingSeconds = totalSeconds; // Initialise the global countdown number
// Display the timer and start the countdown
updateCountDown();
intervalId = setInterval(updateCountDown, 1000);
}| intervalId = setInterval(() => { | ||
| if (remainingSeconds <= 0) { | ||
| clearInterval(intervalId); | ||
| return; | ||
| } | ||
| remainingSeconds -= 1; | ||
| updateDisplayedTime(remainingSeconds); | ||
|
|
||
| if (remainingSeconds <= 0) { | ||
| finishCountDown(); | ||
| } | ||
| }, 1000); |
There was a problem hiding this comment.
This is supposed to be "continuing the countdown". So it can be replaced by
updateCountDown();
intervalId = setInterval(updateCountDown, 1000);Note: Currently, finishCountDown() is not called when remainingSeconds reaches zero.
Learners, PR Template
Self checklist
Changelist
implemented an alarm clock that counts down from the input value