forked from hellomayuko/Pomodoro-Countdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.js
121 lines (98 loc) · 2.62 KB
/
countdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const secondsInaMinute = 60;
const minutes_25 = secondsInaMinute * 25;
const minutes_50 = secondsInaMinute * 50;
const minutes_10 = secondsInaMinute * 10;
const seconds_10 = 10; //for testing
let interval;
let isPaused = true;
let countdownWasStarted = false;
let pomodoroDuration = minutes_50; //by default
let timeLeftInSeconds = 0;
// Button Handlers
function updateDuration() {
//The pomodoro duration is by default 50, but we can change to 25!
if(pomodoroDuration == minutes_50 ) {
pomodoroDuration = minutes_25;
} else if(pomodoroDuration == minutes_25) {
pomodoroDuration = minutes_10;
} else {
pomodoroDuration = minutes_50;
}
timeLeftInSeconds = pomodoroDuration
updateTimeString()
}
function playPauseCountdown() {
isPaused = !isPaused
updatePlayPauseButton();
if(!countdownWasStarted) {
//This function could be called after initiating the timer,
//so we need to differentiate when its start vs pause vs resume
resetCountdown()
updateTimeString()
}
countdownWasStarted = true
if(isPaused) {
stopCountdown()
} else {
// Update the count down every 1 second
interval = setInterval(updateCountdown, 1000);
}
}
function restartCountdown() {
//When we reset the countdown, stop the interval and reset things back to normal
stopCountdown()
resetCountdown()
isPaused = true
updatePlayPauseButton()
updateTimeString()
}
// Biz Logic
function updateCountdown() {
if(isPaused) {
return
}
timeLeftInSeconds--;
updateTimeString();
if(timeLeftInSeconds == 0) {
playYoScott()
stopCountdown()
isPaused = true
updatePlayPauseButton()
}
}
function pauseCountdown() {
isPaused = !isPaused;
}
function stopCountdown() {
clearInterval(interval)
}
function resetCountdown() {
isPaused = false
timeLeftInSeconds = pomodoroDuration
}
// View Updates
function updatePlayPauseButton() {
let playPauseImageSrc;
if(isPaused) {
playPauseImageSrc = "icons8-play-50.png"
} else {
playPauseImageSrc = "icons8-pause-32.png"
}
document.getElementById("playPause").src = playPauseImageSrc;
}
function updateTimeString() {
let minutes = Math.floor(timeLeftInSeconds / secondsInaMinute);
let seconds = timeLeftInSeconds % secondsInaMinute;
if(seconds < 10) {
secondsString = "0" + seconds
} else {
secondsString = seconds
}
// Output the result in an element with id="demo"
document.getElementById("countdown").innerHTML = minutes + ":" + secondsString;
}
function playYoScott() {
var yoScottAudio = document.getElementById("yoScottAudio");
yoScottAudio.play();
document.getElementById("countdown").innerHTML = "DONE";
}