|
1 | | -function setAlarm() {} |
| 1 | + |
| 2 | + |
| 3 | +// function setAlarm() {} |
| 4 | + |
| 5 | +let timeLeft = 0; |
| 6 | +let timer = null; |
| 7 | +let flashing = null; |
| 8 | + |
| 9 | +// DOM references |
| 10 | +const display = document.getElementById("timeRemaining"); |
| 11 | +const setButton = document.getElementById("set"); |
| 12 | +const stopButton = document.getElementById("stop"); |
| 13 | + |
| 14 | +// Event listeners |
| 15 | +setButton.addEventListener("click", setAlarm); |
| 16 | +stopButton.addEventListener("click", stopAlarm); |
| 17 | + |
| 18 | +// Show 00:00 on load |
| 19 | +updateDisplay(0); |
| 20 | + |
| 21 | +// ------------------------------- |
| 22 | +// FUNCTIONS |
| 23 | +// ------------------------------- |
| 24 | + |
| 25 | +function setAlarm() { |
| 26 | + const input = document.getElementById("alarmSet").value; |
| 27 | + const parsed = parseInt(input, 10); |
| 28 | + |
| 29 | + if (isNaN(parsed) || parsed < 0) return; |
| 30 | + |
| 31 | + timeLeft = parsed; |
| 32 | + |
| 33 | + // Update display immediately |
| 34 | + updateDisplay(timeLeft); |
| 35 | + |
| 36 | + // Clear previous countdown |
| 37 | + clearInterval(timer); |
| 38 | + |
| 39 | + // Start countdown every 1000ms |
| 40 | + timer = setInterval(() => { |
| 41 | + if (timeLeft > 0) { |
| 42 | + timeLeft--; |
| 43 | + updateDisplay(timeLeft); |
| 44 | + } |
| 45 | + |
| 46 | + if (timeLeft === 0) { |
| 47 | + clearInterval(timer); |
| 48 | + startAlarm(); |
| 49 | + } |
| 50 | + }, 1000); |
| 51 | +} |
| 52 | + |
| 53 | +function updateDisplay(seconds) { |
| 54 | + const mins = String(Math.floor(seconds / 60)).padStart(2, "0"); |
| 55 | + const secs = String(seconds % 60).padStart(2, "0"); |
| 56 | + display.innerText = `Time Remaining: ${mins}:${secs}`; |
| 57 | +} |
| 58 | + |
| 59 | +function startAlarm() { |
| 60 | + if (typeof playAlarm === "function") playAlarm(); |
| 61 | + |
| 62 | + // Flashing background |
| 63 | + flashing = setInterval(() => { |
| 64 | + document.body.style.backgroundColor = |
| 65 | + document.body.style.backgroundColor === "red" ? "orange" : "red"; |
| 66 | + }, 300); |
| 67 | +} |
| 68 | + |
| 69 | +function stopAlarm() { |
| 70 | + if (typeof pauseAlarm === "function") pauseAlarm(); |
| 71 | + |
| 72 | + clearInterval(flashing); |
| 73 | + flashing = null; |
| 74 | + document.body.style.backgroundColor = ""; |
| 75 | +} |
| 76 | + |
| 77 | +module.exports= setAlarm; |
| 78 | + |
2 | 79 |
|
3 | 80 | // DO NOT EDIT BELOW HERE |
4 | 81 |
|
|
0 commit comments