-
-
Notifications
You must be signed in to change notification settings - Fork 279
Sheffield | 26-ITP-Jan | Mona-Eltantawy | Sprint 3| Alarm clock #1158
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
d598894
3eaa8c0
a204333
384a4dd
7210f43
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,17 +1,48 @@ | ||
| function setAlarm() {} | ||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
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 think your code should be above this line |
||
| let timeRemaining = 0; | ||
| let countdownInterval = null; | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet").value; | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", () => { | ||
| setAlarm(); | ||
| }); | ||
| timeRemaining = parseInt(input, 10); | ||
|
|
||
| if (isNaN(timeRemaining) || timeRemaining <= 0) { | ||
| alert("Please enter a valid number greater than 0"); | ||
|
Comment on lines
+11
to
+12
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. Nice error handling. It's important to validate user input |
||
| return; | ||
| } | ||
|
|
||
| updateDisplay(); | ||
|
|
||
| if (countdownInterval) { | ||
| clearInterval(countdownInterval); | ||
| } | ||
|
|
||
| countdownInterval = setInterval(() => { | ||
| timeRemaining--; | ||
|
|
||
| updateDisplay(); | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
| if (timeRemaining <= 0) { | ||
| clearInterval(countdownInterval); | ||
| playAlarm(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| function updateDisplay() { | ||
| const display = document.getElementById("timeRemaining"); | ||
|
|
||
| const minutes = String(Math.floor(timeRemaining / 60)).padStart(2, "0"); | ||
| const seconds = String(timeRemaining % 60).padStart(2, "0"); | ||
|
|
||
| display.textContent = `Time Remaining: ${minutes}:${seconds}`; | ||
| } | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", setAlarm); | ||
| document.getElementById("stop").addEventListener("click", pauseAlarm); | ||
| } | ||
|
|
||
| function playAlarm() { | ||
|
|
@@ -20,6 +51,11 @@ function playAlarm() { | |
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| audio.currentTime = 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. Nice that you reset the audio time here |
||
|
|
||
| if (countdownInterval) { | ||
| clearInterval(countdownInterval); | ||
| } | ||
| } | ||
|
|
||
| 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.
The html title should say "Alarm clock app"