-
-
Notifications
You must be signed in to change notification settings - Fork 321
London | 26-ITP-May | Dipa Sarker | Sprint 3 | Alarm Clock App #1294
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 |
|---|---|---|
|
|
@@ -23,3 +23,64 @@ function pauseAlarm() { | |
| } | ||
|
|
||
| window.onload = setup; | ||
|
|
||
| const timeRemainingElement = document.getElementById("timeRemaining"); | ||
| const alarmSetElement = document.getElementById("alarmSet"); | ||
| const setButtonElement = document.getElementById("set"); | ||
| const stopButtonElement = document.getElementById("stop"); | ||
| const bodyElement = document.querySelector("body"); | ||
| const alarmSoundElement = document.getElementById("alarmSound"); | ||
|
|
||
| let remainingTime; | ||
| let intervalID; | ||
|
|
||
| function triggerAlarm() { | ||
| bodyElement.style.backgroundColor = "yellow"; | ||
| alarmSoundElement.play(); | ||
| } | ||
|
|
||
| function setAlarm() { | ||
|
LonMcGregor marked this conversation as resolved.
|
||
| clearInterval(intervalID); | ||
| const alarmTime = alarmSetElement.value; | ||
| if (alarmTime === "") { | ||
| return; | ||
| } //input validation (for emptry input) | ||
| remainingTime = Number(alarmTime); | ||
| if (remainingTime < 0) { | ||
| return; | ||
| } //input validation(for -ve number input) | ||
| const showTimeMinutes = Math.floor(remainingTime / 60) | ||
| .toString() | ||
| .padStart(2, "0"); | ||
| const showTimeSeconds = (remainingTime % 60).toString().padStart(2, "0"); | ||
| timeRemainingElement.innerText = showTimeMinutes + ":" + showTimeSeconds; | ||
| if (remainingTime === 0) { | ||
|
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. Do you properly finish the timer countdown after it hits zero?
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. Sorry, I am unclear about your question. But I think I finished the countdown after hitting zero at here: function countDown() { Which means:
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. I see, I was confused that you have a triggerAlarm in the setAlarm function. if you also have it in the countDown function, is it necessary to have it in the setAlarm function also?
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. if (remainingTime <= 0) { |
||
| triggerAlarm(); //valid input, when input = 0 then immediately triggers alarm | ||
| } else { | ||
| intervalID = setInterval(countDown, 1000); | ||
| } //valid input, when input = 10 then countdown starts | ||
| } | ||
| setButtonElement.addEventListener("click", setAlarm); | ||
| function countDown() { | ||
| if (remainingTime <= 0) { | ||
| clearInterval(intervalID); | ||
| triggerAlarm(); | ||
| return; | ||
| } /*to avoid any chance of -ve counting after countdown reached 0 from 10. | ||
| when countdown = 0 alarm triggers immediately and returns from the code */ | ||
|
|
||
| remainingTime -= 1; | ||
| const remainingMinutes = Math.floor(remainingTime / 60) | ||
| .toString() | ||
| .padStart(2, "0"); | ||
| const remainingSeconds = (remainingTime % 60).toString().padStart(2, "0"); | ||
| timeRemainingElement.innerText = remainingMinutes + ":" + remainingSeconds; | ||
| } | ||
|
|
||
| function stopAlarm() { | ||
| clearInterval(intervalID); | ||
| alarmSoundElement.pause(); | ||
| alarmSoundElement.currentTime = 0; | ||
| bodyElement.style.backgroundColor = ""; | ||
| } | ||
| stopButtonElement.addEventListener("click", stopAlarm); | ||
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.
A bit more spacing between functions / improved formatting would help readability.
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.
Actually, I used prettier tool according to the style guide for formatting. Should I provide extra spaces in between functions?
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.
A clear line between functions is a good idea
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 placed one line inside functions.