Skip to content

Commit 57bdb92

Browse files
committed
added flashing
1 parent 0f1d940 commit 57bdb92

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,65 @@ const backgroundColor = document.querySelector("html");
2525
console.log(backgroundColor, "<------Background Color");
2626

2727
let intervalId;
28+
let inputInfo = 0;
29+
30+
function timeFormat(time) {
31+
const mins = String(Math.floor(time / 60)).padStart(2, "0");
32+
const seconds = String(time % 60).padStart(2, "0");
33+
timeRemainInfo.textContent = `Time Remaining: ${mins}:${seconds}`;
34+
}
2835

2936
function setAlarm() {
30-
let inputInfo = Number(alarmInputArea.value);
31-
if (inputInfo < 1) {
32-
console.log("You need to input a value");
33-
window.alert("You need to input a value!");
37+
inputInfo = Number(alarmInputArea.value);
38+
if (inputInfo < 1 || isNaN(inputInfo)) {
39+
console.log("You need to input a value in seconds!");
40+
window.alert("You need to input a value in seconds!");
3441
return;
3542
}
3643
alarmInputArea.value = "";
37-
const mins = String(Math.floor(inputInfo / 60)).padStart(2, "0");
38-
const seconds = String(inputInfo % 60).padStart(2, "0");
39-
timeRemainInfo.textContent = `Time Remaining: ${mins}:${seconds}`;
44+
timeFormat(inputInfo);
4045

4146
intervalId = setInterval(function () {
42-
if (inputInfo <= 0) {
47+
inputInfo--;
48+
if (inputInfo === 0) {
4349
clearInterval(intervalId);
44-
playAlarm();
50+
timeFormat(0);
51+
window.playAlarm();
4552
changeBackgroundColorFlashing();
46-
return;
4753
} else {
48-
inputInfo--;
49-
const mins = String(Math.floor(inputInfo / 60)).padStart(2, "0");
50-
const seconds = String(inputInfo % 60).padStart(2, "0");
51-
timeRemainInfo.textContent = `Time Remaining: ${mins}:${seconds}`;
54+
timeFormat(inputInfo);
5255
}
5356
}, 1000);
5457
}
5558

5659
//setAlarmButton.addEventListener("click", function setAlarm() {
5760
//console.log("click event is firing...");
5861
//});
62+
let flashColor = ["yellow", "pink", "lightgrey", "green"];
63+
let flashIntervalId;
64+
let colorIndex = 0;
5965

6066
setStopButton.addEventListener("click", function stopFlashing() {
6167
console.log("click event is firing...");
6268
clearInterval(flashIntervalId);
6369
backgroundColor.style.backgroundColor = "";
64-
pauseAlarm();
70+
window.pauseAlarm();
6571
});
6672

6773
//function changeBackgroundColor() {
6874
//backgroundColor.style.backgroundColor = "lightblue";
6975
//}
7076

71-
let flashColor = ["yellow", "pink", "lightgrey", "green"];
72-
let flashIntervalId;
73-
let colorIndex = 0;
74-
7577
function changeBackgroundColorFlashing() {
7678
if (flashIntervalId) clearInterval(flashIntervalId);
7779
flashIntervalId = setInterval(function () {
7880
backgroundColor.style.backgroundColor = flashColor[colorIndex];
7981
colorIndex = (colorIndex + 1) % flashColor.length;
8082
}, 1000);
83+
//stopFlashing();
8184
}
85+
window.playAlarm = playAlarm;
86+
window.pauseAlarm = pauseAlarm;
8287

8388
// DO NOT EDIT BELOW HERE
8489

0 commit comments

Comments
 (0)