Skip to content

Commit 3c1df8c

Browse files
refactor updateCountDown function to use shared remainingSeconds logic for start and unpause
1 parent 8c26043 commit 3c1df8c

1 file changed

Lines changed: 16 additions & 24 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
let intervalId;
22
let isPaused;
33
let remainingSeconds = 0;
4+
5+
function updateCountDown() {
6+
updateDisplayedTime(remainingSeconds);
7+
8+
if (remainingSeconds <= 0) {
9+
finishCountDown();
10+
return;
11+
}
12+
13+
remainingSeconds -= 1;
14+
}
15+
416
function setAlarm() {
517
const alarmSetEl = document.getElementById("alarmSet");
6-
let totalSeconds = +alarmSetEl.value;
18+
const totalSeconds = +alarmSetEl.value;
719

820
if (
921
totalSeconds <= 0 ||
@@ -15,17 +27,7 @@ function setAlarm() {
1527
}
1628
cleanInitialState();
1729

18-
function updateCountDown() {
19-
remainingSeconds = totalSeconds;
20-
updateDisplayedTime(remainingSeconds);
21-
22-
if (totalSeconds <= 0) {
23-
finishCountDown();
24-
return;
25-
}
26-
27-
totalSeconds -= 1;
28-
}
30+
remainingSeconds = totalSeconds;
2931

3032
updateCountDown();
3133
intervalId = setInterval(updateCountDown, 1000);
@@ -39,18 +41,8 @@ function pauseCountDown(e) {
3941
} else {
4042
e.target.textContent = "Pause";
4143
isPaused = false;
42-
intervalId = setInterval(() => {
43-
if (remainingSeconds <= 0) {
44-
clearInterval(intervalId);
45-
return;
46-
}
47-
remainingSeconds -= 1;
48-
updateDisplayedTime(remainingSeconds);
49-
50-
if (remainingSeconds <= 0) {
51-
finishCountDown();
52-
}
53-
}, 1000);
44+
updateCountDown();
45+
intervalId = setInterval(updateCountDown, 1000);
5446
}
5547
}
5648

0 commit comments

Comments
 (0)