-
-
Notifications
You must be signed in to change notification settings - Fork 168
Glasgow | 25-ITP-SEP | Shreef Ibrahim | Sprint 3| Alarm clock app #795
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
cf3676f
8caafb9
4707429
b450711
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,25 +1,104 @@ | ||
| function setAlarm() {} | ||
| let timer; | ||
| let timeLeft = 0; | ||
| let paused = false; | ||
| const pauseBtn = document.getElementById("pauseAndResume"); | ||
| const setBtn = document.getElementById("set"); | ||
| const inputField = document.getElementById("alarmSet"); | ||
| const audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| // update alarm | ||
| function updateDisplay() { | ||
| const timeDisplay = document.getElementById("timeRemaining"); | ||
| const mm = String(Math.floor(timeLeft / 60)).padStart(2, "0"); | ||
| const ss = String(timeLeft % 60).padStart(2, "0"); | ||
| timeDisplay.textContent = `Time Remaining: ${mm}:${ss}`; | ||
| } | ||
|
|
||
| // stop or start flashing | ||
| let flashingInterval; | ||
| const flashingColors = ["#FF4136", "#2ECC40", "#0074D9", "#FFDC00"]; | ||
| function startFlashingBackground() { | ||
| let colorIndex = 0; | ||
| flashingInterval = setInterval(() => { | ||
| document.body.style.backgroundColor = flashingColors[colorIndex]; | ||
| colorIndex = (colorIndex + 1) % flashingColors.length; | ||
| }, 200); | ||
| } | ||
| function stopFlashingBackground() { | ||
| clearInterval(flashingInterval); | ||
| document.body.style.backgroundColor = ""; | ||
| } | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
| // start countDown | ||
| function startCountdown() { | ||
| clearInterval(timer); | ||
| timer = setInterval(() => { | ||
| if (--timeLeft <= 0) { | ||
| clearInterval(timer); | ||
| startFlashingBackground(); | ||
| playAlarm(); | ||
| pauseBtn.style.display = "none"; | ||
| updateDisplay(); | ||
| } else { | ||
| updateDisplay(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", () => { | ||
| setAlarm(); | ||
| }); | ||
| // set up alarm | ||
| function setAlarm() { | ||
| const val = parseInt(inputField.value); | ||
|
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. What if 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. When inputField.value is empty, the parseInt("") returns NaN, 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 mean you already have it in your code or you are going to push? I don't see validation against NaN, etc. 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 have already used it thing i did not push properly 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. Ah okay, that happens 🙂 |
||
| if (!val || val <= 0) { | ||
| alert("Please enter a valid number of seconds."); | ||
| return; | ||
| } | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
| timeLeft = val; | ||
| updateDisplay(); | ||
| startCountdown(); | ||
|
|
||
| setBtn.style.display = "none"; | ||
| pauseBtn.style.display = "inline"; | ||
| inputField.style.display = "none"; | ||
| } | ||
|
|
||
| function playAlarm() { | ||
| audio.currentTime = 0; | ||
| audio.play(); | ||
| } | ||
| // pause or resume alarm | ||
| function pauseResumeHandler() { | ||
| if (!paused) { | ||
| clearInterval(timer); | ||
| pauseBtn.textContent = "Resume"; | ||
| } else { | ||
| startCountdown(); | ||
| pauseBtn.textContent = "Pause"; | ||
| } | ||
| paused = !paused; | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| // stop alarm | ||
| function stopAlarm() { | ||
| clearInterval(timer); | ||
| audio.pause(); | ||
| audio.currentTime = 0; | ||
| stopFlashingBackground(); | ||
| timeLeft = 0; | ||
| updateDisplay(); | ||
|
|
||
| setBtn.style.display = "inline"; | ||
| pauseBtn.style.display = "none"; | ||
| inputField.style.display = "inline"; | ||
| pauseBtn.textContent = "Pause"; | ||
| paused = false; | ||
| } | ||
|
|
||
| function setup() { | ||
| const stopBtn = document.getElementById("stop"); | ||
| setBtn.addEventListener("click", setAlarm); | ||
| pauseBtn.addEventListener("click", pauseResumeHandler); | ||
| stopBtn.addEventListener("click", stopAlarm); | ||
| } | ||
|
|
||
| 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.
It's better to keep variable local if it's not used elsewhere. Makes your code cleaner and reduces possibility of bugs due to accidental change to your variable.