generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 223
Manchester | 25-ITP-Sep | Mahtem T. Mengstu | Sprint 3 | Alarmclock #916
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aef7fe1
Modified the index.html adding necessary buttons
Mahtem 084497d
Style modified
Mahtem db4559d
Alarm clock function implemented
Mahtem d46d718
Index.html <script src="alarmclock.js" type="module"></script>
Mahtem 6a48187
style.css modified to include flashing background
Mahtem 102e20d
alarmclock.js modified
Mahtem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,126 @@ | ||
| function setAlarm() {} | ||
| // -------- Alarm Clock Implementation -------- | ||
| let countdownInterval = null; | ||
| let timeLeft = 0; | ||
| let paused = false; | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| // -------- Main Functions -------- | ||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); | ||
| const secondsInput = parseInt(input.value, 10); | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
| // Ignore invalid or zero/negative inputs | ||
| if (isNaN(secondsInput) || secondsInput <= 0) return; | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", () => { | ||
| setAlarm(); | ||
| }); | ||
| // Reset previous alarm | ||
| resetAlarm(); | ||
|
|
||
| timeLeft = secondsInput; | ||
| updateHeading(timeLeft); | ||
|
|
||
| input.disabled = true; | ||
|
|
||
| countdownInterval = setInterval(() => { | ||
| if (!paused) { | ||
| timeLeft--; | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
| if (timeLeft <= 0) { | ||
| clearInterval(countdownInterval); | ||
| countdownInterval = null; | ||
| updateHeading(0); | ||
| playAlarm(); | ||
| startFlashing(); | ||
| input.disabled = false; | ||
| } else { | ||
| updateHeading(timeLeft); | ||
| } | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| paused = true; | ||
| audio.pause(); | ||
| stopFlashing(); | ||
| } | ||
|
|
||
| function resumeAlarm() { | ||
| if (timeLeft > 0) paused = false; | ||
| } | ||
|
|
||
| function stopAlarm() { | ||
| resetAlarm(); // fully stop everything | ||
| } | ||
|
|
||
| function resetAlarm() { | ||
| // Stop countdown | ||
| paused = false; | ||
| if (countdownInterval) { | ||
| clearInterval(countdownInterval); | ||
| countdownInterval = null; | ||
| } | ||
|
|
||
| // Stop flashing background | ||
| stopFlashing(); | ||
|
|
||
| // Stop alarm sound if playing | ||
| if (audio) { | ||
| audio.pause(); | ||
| audio.currentTime = 0; | ||
| } | ||
|
|
||
| // Reset countdown display | ||
| timeLeft = 0; | ||
| updateHeading(0); | ||
|
|
||
| // Clear and re-enable input | ||
| const input = document.getElementById("alarmSet"); | ||
| if (input) { | ||
| input.value = ""; | ||
| input.disabled = false; | ||
| } | ||
| } | ||
|
|
||
| // -------- Helper Functions -------- | ||
| function updateHeading(seconds) { | ||
| const heading = document.getElementById("timeRemaining"); | ||
| const mins = Math.floor(seconds / 60) | ||
| .toString() | ||
| .padStart(2, "0"); | ||
| const secs = (seconds % 60).toString().padStart(2, "0"); | ||
| heading.innerText = `Time Remaining: ${mins}:${secs}`; | ||
| } | ||
|
|
||
| // ---------------- Flashing Screen (CSS-based) ---------------- | ||
| function startFlashing() { | ||
| document.body.classList.add("alarm-flashing"); | ||
| } | ||
|
|
||
| function stopFlashing() { | ||
| document.body.classList.remove("alarm-flashing"); | ||
| document.body.style.backgroundColor = "white"; | ||
| } | ||
|
|
||
| // ---------------- Audio ---------------- | ||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| function playAlarm() { | ||
| audio.play(); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| function pauseAlarmSound() { | ||
| audio.pause(); | ||
| } | ||
|
|
||
| // ---------------- Setup Event Listeners ---------------- | ||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", setAlarm); | ||
| document.getElementById("pause").addEventListener("click", pauseAlarm); | ||
| document.getElementById("resume").addEventListener("click", resumeAlarm); | ||
| document.getElementById("stop").addEventListener("click", stopAlarm); | ||
| document.getElementById("reset").addEventListener("click", resetAlarm); | ||
| } | ||
|
|
||
| window.onload = setup; | ||
|
|
||
|
|
||
| // alarmclock.js modified | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,74 @@ | ||
| /* Center everything on the page */ | ||
| .centre { | ||
| position: fixed; | ||
| top: 50%; | ||
| left: 50%; | ||
| -webkit-transform: translate(-50%, -50%); | ||
| transform: translate(-50%, -50%); | ||
| text-align: center; | ||
| font-family: Arial, sans-serif; | ||
| } | ||
|
|
||
| #alarmSet { | ||
| margin: 20px; | ||
| /* Heading */ | ||
| h1 { | ||
| text-align: center; | ||
| font-size: 2em; | ||
| margin-bottom: 20px; | ||
| transition: background-color 0.2s ease; | ||
| } | ||
|
|
||
| h1 { | ||
| /* Input field */ | ||
| #alarmSet { | ||
| width: 80px; | ||
| padding: 5px; | ||
| font-size: 1em; | ||
| margin-bottom: 20px; | ||
| text-align: center; | ||
| } | ||
|
|
||
| /* Buttons container */ | ||
| .buttons { | ||
| display: flex; | ||
| justify-content: center; | ||
| flex-wrap: wrap; | ||
| gap: 10px; /* space between buttons */ | ||
| margin-top: 10px; | ||
| } | ||
|
|
||
| /* Buttons */ | ||
| button { | ||
| padding: 10px 20px; | ||
| font-size: 1em; | ||
| cursor: pointer; | ||
| border-radius: 5px; | ||
| border: none; | ||
| background-color: #007bff; | ||
| color: white; | ||
| transition: background-color 0.2s ease, transform 0.1s ease; | ||
| } | ||
|
|
||
| button:hover { | ||
| background-color: #0056b3; | ||
| transform: scale(1.05); | ||
| } | ||
|
|
||
| button:active { | ||
| transform: scale(0.95); | ||
| } | ||
|
|
||
| /* Input and buttons spacing */ | ||
| input, | ||
| button { | ||
| margin: 5px; | ||
| } | ||
|
|
||
| /* Flashing background */ | ||
| @keyframes alarm-flash { | ||
| 0%, 100% { background-color: white; } | ||
| 50% { background-color: #add8e6; } | ||
| } | ||
|
|
||
| .alarm-flashing { | ||
| animation: alarm-flash 0.5s steps(1, end) infinite; | ||
| } | ||
|
|
||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could consider implementing the "flashing background" effect using only CSS, and then add/remove a class to start/stop flashing.
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.
Thank you @cjyuan, it has been implemented using only css.
function startFlashing() {
document.body.classList.add("alarm-flashing");
}
function stopFlashing() {
document.body.classList.remove("alarm-flashing");
document.body.style.backgroundColor = "white";
}