-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
aef7fe1
084497d
db4559d
d46d718
6a48187
102e20d
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,95 @@ | ||
| function setAlarm() {} | ||
| // -------- Alarm Clock Implementation -------- | ||
| let countdownInterval = null; | ||
| let flashInterval = null; | ||
| let timeLeft = 0; | ||
| let paused = false; | ||
|
|
||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); | ||
| const heading = document.getElementById("timeRemaining"); | ||
|
|
||
| const secondsInput = parseInt(input.value, 10); | ||
|
|
||
| if (isNaN(secondsInput) || secondsInput < 0) return; | ||
|
|
||
| // Reset previous timer if any | ||
| if (countdownInterval) clearInterval(countdownInterval); | ||
| stopFlashing(); | ||
| paused = false; | ||
| timeLeft = secondsInput; | ||
|
|
||
| updateHeading(timeLeft); | ||
|
|
||
| countdownInterval = setInterval(() => { | ||
| if (!paused) { | ||
| timeLeft--; | ||
| if (timeLeft <= 0) { | ||
| clearInterval(countdownInterval); | ||
| countdownInterval = null; | ||
| updateHeading(0); | ||
| playAlarm(); | ||
| startFlashing(); | ||
| } else { | ||
| updateHeading(timeLeft); | ||
| } | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| paused = true; | ||
| if (audio) audio.pause(); | ||
|
||
| stopFlashing(); | ||
| } | ||
|
|
||
| function resumeAlarm() { | ||
| if (timeLeft > 0) paused = false; | ||
| } | ||
|
|
||
| function stopAlarm() { | ||
| paused = true; | ||
| if (audio) audio.pause(); | ||
| stopFlashing(); | ||
| clearInterval(countdownInterval); | ||
| countdownInterval = null; | ||
| timeLeft = 0; | ||
| updateHeading(0); | ||
| } | ||
|
|
||
| function resetAlarm() { | ||
| paused = false; | ||
| clearInterval(countdownInterval); | ||
| countdownInterval = null; | ||
| stopFlashing(); | ||
| timeLeft = 0; | ||
| updateHeading(0); | ||
| } | ||
|
|
||
| // -------- 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 ---------------- | ||
| function startFlashing() { | ||
| const body = document.body; | ||
| let isBlue = false; | ||
| flashInterval = setInterval(() => { | ||
| body.style.backgroundColor = isBlue ? "white" : "#add8e6"; | ||
| isBlue = !isBlue; | ||
| }, 500); | ||
| } | ||
|
|
||
| function stopFlashing() { | ||
| clearInterval(flashInterval); | ||
| flashInterval = null; | ||
| document.body.style.backgroundColor = "white"; | ||
| } | ||
|
Comment on lines
94
to
101
Contributor
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 consider implementing the "flashing background" effect using only CSS, and then add/remove a class to start/stop flashing.
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.
Thank you @cjyuan, it has been implemented using only css. function stopFlashing() { |
||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -9,16 +100,28 @@ function setup() { | |
| setAlarm(); | ||
| }); | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| document.getElementById("pause").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
|
|
||
| document.getElementById("resume").addEventListener("click", () => { | ||
| resumeAlarm(); | ||
| }); | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| stopAlarm(); | ||
| }); | ||
|
|
||
| document.getElementById("reset").addEventListener("click", () => { | ||
| resetAlarm(); | ||
| }); | ||
| } | ||
|
|
||
| function playAlarm() { | ||
| audio.play(); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| function pauseAlarmSound() { | ||
| audio.pause(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,26 @@ | |
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| <title>Title here</title> | ||
| <title>Alarm clock app</title> | ||
| </head> | ||
| <body> | ||
| <div class="centre"> | ||
| <h1 id="timeRemaining">Time Remaining: 00:00</h1> | ||
| <label for="alarmSet">Set time to:</label> | ||
|
|
||
| <label for="alarmSet">Set time in seconds:</label> | ||
| <input id="alarmSet" type="number" /> | ||
|
|
||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| <div class="buttons"> | ||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="pause" type="button">Pause Alarm</button> | ||
| <button id="resume" type="button">Resume Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| <button id="reset" type="button">Reset Alarm</button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <script src="alarmclock.js"></script> | ||
|
||
| </body> | ||
| </html> | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,63 @@ | ||
| /* 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; | ||
| } | ||
|
|
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 also consider calling
resetAlarm()(even if it performs something extra).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 has been made to reset everything completely.