-
-
Notifications
You must be signed in to change notification settings - Fork 194
Manchester| 25-ITP-Sep| Fithi Teklom| Sprint 3 | Alarm Clock #908
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
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,101 @@ | ||
| function setAlarm() {} | ||
|
|
||
|
|
||
| // function setAlarm() {} | ||
|
|
||
| let timeLeft = 0; | ||
| let timer = null; | ||
| let flashing = null; | ||
|
|
||
| // DOM references | ||
| window.addEventListener("DOMContentLoaded", () => { | ||
| // const display = document.getElementById("timeRemaining"); | ||
| const setButton = document.getElementById("set"); | ||
| const stopButton = document.getElementById("stop"); | ||
|
|
||
| // Event listeners | ||
| // if (setButton) setButton.addEventListener("click", () => playAlarm()); | ||
| if (stopButton) stopButton.addEventListener("click", stopAlarm); | ||
|
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. Callback added using |
||
|
|
||
| // Show 00:00 on load | ||
| updateDisplay(0); | ||
| }); | ||
|
|
||
| // ------------------------------- | ||
| // FUNCTIONS | ||
| // ------------------------------- | ||
|
|
||
| function setAlarm() { | ||
| const inputEl = document.getElementById("alarmSet"); | ||
| const input = inputEl.value.trim(); | ||
|
|
||
| // Check empty input | ||
| if (input === "") { | ||
| alert("Please enter a number of seconds."); | ||
| return; | ||
| } | ||
|
|
||
| const parsed = parseInt(input, 10); | ||
|
|
||
| // Check invalid or negative number | ||
| if (isNaN(parsed) || parsed < 0) { | ||
| alert("Please enter a valid non-negative number."); | ||
| return; | ||
| } | ||
|
|
||
| timeLeft = parsed; | ||
|
|
||
| // Update display immediately | ||
| updateDisplay(timeLeft); | ||
|
|
||
| // Clear previous countdown | ||
| clearInterval(timer); | ||
|
|
||
|
Comment on lines
+50
to
+52
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. Can you think of anything else that should also be reset before starting a new countdown clock? |
||
| // Start countdown every 1000ms | ||
| timer = setInterval(() => { | ||
| if (timeLeft > 0) { | ||
| timeLeft--; | ||
| updateDisplay(timeLeft); | ||
| } | ||
|
|
||
| if (timeLeft === 0) { | ||
| clearInterval(timer); | ||
| startAlarm(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
Comment on lines
+54
to
+65
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. The alarm will start 1 second after the alarm is set when the input is either 0 or 1. |
||
|
|
||
| function updateDisplay(seconds) { | ||
| const mins = String(Math.floor(seconds / 60)).padStart(2, "0"); | ||
| const secs = String(seconds % 60).padStart(2, "0"); | ||
|
|
||
| const display = document.getElementById("timeRemaining"); | ||
| if (!display) return; | ||
| display.textContent = `Time Remaining: ${mins}:${secs}`; | ||
| } | ||
|
|
||
| function startAlarm() { | ||
| playAlarm(); | ||
|
|
||
| // Flashing background | ||
| if (!flashing){ | ||
| flashing = setTimeout(() => { | ||
| document.body.style.backgroundColor = | ||
| document.body.style.backgroundColor === "red" ? "orange" : "red"; | ||
| }, 300); | ||
| } | ||
| } | ||
|
|
||
| function stopAlarm() { | ||
| if (typeof pauseAlarm === "function") pauseAlarm(); | ||
|
|
||
| clearInterval(flashing); | ||
| flashing = null; | ||
| document.body.style.backgroundColor = ""; | ||
| } | ||
|
|
||
|
|
||
| // module.exports= setAlarm; | ||
|
|
||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** @type {import('jest').Config} */ | ||
| module.exports = { | ||
| testEnvironment: "jsdom", | ||
| verbose: true, | ||
| testMatch: ["**/*.test.js"], | ||
| }; |
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.
Do you still need this variable?