-
-
Notifications
You must be signed in to change notification settings - Fork 286
Glasgow | Jan-26 | Elisabeth Matulian | Sprint 3 | Alarm clock app #1020
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
Changes from 2 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,44 @@ | ||
| function setAlarm() {} | ||
| const timeRemaining = document.querySelector("#timeRemaining"); | ||
|
|
||
| let intervalId; //declared here so that I can use it in line-17, before asigning on line 21. | ||
|
|
||
| function setAlarm() { | ||
| // I will use the function showTime twice, one for showing the time just after the user clicks the set button and another one for updating the time every second in countdown function | ||
| function showTime(inputValue) { | ||
| if (inputValue >= 0) { | ||
|
Elisabeth-Matulian marked this conversation as resolved.
Outdated
|
||
| // getting the mins & sec from the inputValue(line19) in appropriate format | ||
| const minutes = Math.floor(inputValue / 60) | ||
| .toString() | ||
| .padStart(2, "0"); | ||
|
Elisabeth-Matulian marked this conversation as resolved.
Outdated
|
||
| const seconds = (inputValue % 60).toString().padStart(2, "0"); | ||
| //Gathering everything together and outputing remaining time | ||
| timeRemaining.innerText = `Time Remaining: ${minutes}:${seconds}`; | ||
| }} | ||
|
Elisabeth-Matulian marked this conversation as resolved.
Outdated
|
||
| //the next line in case if we want to reset the time for alarm | ||
| clearInterval(intervalId); | ||
| // getting user's input value in number format | ||
| let inputValue = Number(document.querySelector("#alarmSet").value); | ||
|
Elisabeth-Matulian marked this conversation as resolved.
Outdated
|
||
| if (inputValue <= 0) { | ||
| timeRemaining.innerText = "Enter a positive number."; | ||
| document.querySelector("#alarmSet").value = ''; | ||
| return; | ||
| } | ||
| showTime(inputValue); | ||
| intervalId = setInterval(countdown, 1000); | ||
|
|
||
| function countdown() { | ||
| if (inputValue >= 0) { | ||
| inputValue--; | ||
| showTime(inputValue); | ||
| } | ||
| // When the inputValue is 0, play the alarm and clear interval so that the function stops initialising | ||
| else { | ||
| playAlarm(); | ||
| clearInterval(intervalId); | ||
| document.querySelector("#alarmSet").value = ''; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -20,6 +60,8 @@ function playAlarm() { | |
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| //I know it's said to not edit this code, I wonder if it's ok to add this line below in order to pause the time before the time runs out. | ||
| clearInterval(intervalId); | ||
| } | ||
|
Comment on lines
67
to
69
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. Excellent catch, and the approach is correct, as a bonus task, though, how can we make this change and add the
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. Could you give me a hint? 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. Sure thing, what if I told you that you can have multiple event listeners for the same element?
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. I still couldn't figure out how to implement it 😵💫 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. You have actually done it, but the code is commented out on lines 43-45. You can indeed register multiple event listeners to the same element as you did; they won't override each other. For what is worth, adding them in the pause alarm did make sense too, but I hope it was an interesting extra step. Let's comment it back it and I will approve :)
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. Oh, so that’s how it works))) I didn’t think it would work, I just left it as a comment so you could see the way I think and point me in the right direction. That’s surely something I’ll remember)) |
||
|
|
||
| window.onload = setup; | ||
Uh oh!
There was an error while loading. Please reload this page.