-
-
Notifications
You must be signed in to change notification settings - Fork 279
London|26-ITP-January|Alexandru Pocovnicu|Sprint 3| alarm clock #1057
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
47f91e5
41537a4
3ab9799
8a4ba11
ca62e47
08fbcbd
aa8df3b
950cd4a
4aafd50
dc665e5
b81719d
8354926
063a0fa
5d64c66
c84e2a9
894313e
3b6db49
11d7474
5209ca9
4251c18
a65ca54
6d09014
6799a86
fbb165f
74c4c23
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,91 @@ | ||
| function setAlarm() {} | ||
| let intervalId; | ||
| let isPaused; | ||
| let remainingSeconds = 0; | ||
| function setAlarm() { | ||
|
|
||
| const pauseButton = document.getElementById("pause-button"); | ||
| pauseButton.textContent = "Pause"; | ||
| pauseButton.removeEventListener("click",pauseCountDown) | ||
| pauseButton.addEventListener("click", pauseCountDown); | ||
|
|
||
| const alarmSetEl = document.getElementById("alarmSet"); | ||
| const timeRemainingEl = document.getElementById("timeRemaining"); | ||
| let totalSeconds = +alarmSetEl.value; | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| remainingSeconds = totalSeconds; | ||
|
|
||
| if ( | ||
| totalSeconds <= 0 || | ||
| isNaN(totalSeconds) || | ||
| !Number.isInteger(totalSeconds) | ||
| ) { | ||
| alarmSetEl.value = ""; | ||
| return; | ||
| } | ||
| cleanInitialState(); | ||
|
|
||
| function updateCountDown() { | ||
| remainingSeconds = totalSeconds; | ||
| updateDisplayedTime(remainingSeconds); | ||
|
|
||
| if (totalSeconds <= 0) { | ||
| finishCountDown(); | ||
| return; | ||
| } | ||
|
|
||
| totalSeconds -= 1; | ||
| } | ||
|
|
||
| updateCountDown(); | ||
| intervalId = setInterval(updateCountDown, 1000); | ||
| } | ||
|
|
||
| function pauseCountDown(e) { | ||
| if (isPaused === false) { | ||
| clearInterval(intervalId); | ||
| e.target.textContent = "Unpause"; | ||
| isPaused = true; | ||
| } else { | ||
| e.target.textContent = "Pause"; | ||
| isPaused = false; | ||
| intervalId = setInterval(() => { | ||
| if (remainingSeconds <= 0) { | ||
| clearInterval(intervalId); | ||
| return; | ||
| } | ||
| remainingSeconds -= 1; | ||
| updateDisplayedTime(remainingSeconds); | ||
|
|
||
| if (remainingSeconds <= 0) { | ||
| finishCountDown(); | ||
| } | ||
| }, 1000); | ||
|
Comment on lines
+50
to
+61
Contributor
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. It seems the code here is very similar to those perform in |
||
| } | ||
| } | ||
|
|
||
| function cleanInitialState() { | ||
| isPaused = false; | ||
| clearInterval(intervalId); | ||
| document.body.classList.remove("finish-countdown"); | ||
| updateDisplayedTime(0) | ||
| document.getElementById("alarmSet").value = null; | ||
|
Contributor
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.
|
||
| pauseAlarm(); | ||
| } | ||
|
|
||
| function updateDisplayedTime(totalSeconds) { | ||
| let seconds = totalSeconds % 60; | ||
| let minutes = (totalSeconds - seconds) / 60; | ||
|
|
||
| let paddedSeconds = seconds.toString().padStart(2, "0"); | ||
| let paddedMinutes = minutes.toString().padStart(2, "0"); | ||
| document.getElementById("timeRemaining").innerHTML = | ||
| `Time Remaining: ${paddedMinutes}:${paddedSeconds}`; | ||
| } | ||
|
|
||
| function finishCountDown() { | ||
| document.body.classList.add("finish-countdown"); | ||
| playAlarm(); | ||
| clearInterval(intervalId); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
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.
You can reset the
isPausedflag and the "Pause" button state incleanInitialState(), and add event listener only once on page load.Currently, the same event listener will be added to the button multiple times (once per function call).
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.
but if i move the listener inside cleanInitialState(), which is called everytime setAlarm() is called, isn't that the same thing, how about instead, i remove the listener inside setAlarm just before adding it?
thank you
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.
I mean, perform this in
cleanInitialState()and perform this on page load