forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (114 loc) · 3.89 KB
/
index.js
File metadata and controls
126 lines (114 loc) · 3.89 KB
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
122
123
124
125
126
/**
In this week 's project you'll be making a Pomodoro Clock!
A user can specify how many minutes the timer should be set, and with a click on the play button it starts counting down!If the user wants to pause the timer, they can do so by clicking the pause button.
If the timer is running, the user can 't change the session length anymore
Use at least 3 functions
Display minutes and seconds
If the timer finishes the timer should be replaced by the message: Time 's up!
*
*/
// debugger;
const arrowUp = document.getElementById('up');
const arrowDown = document.getElementById('down');
const playBtn = document.getElementById('play');
const pauseBtn = document.getElementById('pause');
const stopBtn = document.getElementById('stop');
const TimeUpMessage = document.getElementById('time-up-message');
const sessionLength = document.getElementById('session-length');
const minutes = document.getElementById('minutes');
const seconds = document.getElementById('seconds');
const notification = document.getElementById('notification');
const sZero = document.getElementById('s-zero');
const mZero = document.getElementById('m-zero');
const colon = document.getElementById('colon');
let startCountdown;
let timeUp = false;
pauseBtn.disabled = true;
pauseBtn.style.color = 'grey';
playBtn.addEventListener('click', function () {
arrowUp.disabled = true;
arrowDown.disabled = true;
pauseBtn.disabled = false;
pauseBtn.style.color = 'white';
arrowUp.style.color = 'grey';
arrowDown.style.color = 'grey';
minutes.style.color = '#2ed573';
seconds.style.color = '#2ed573';
colon.style.color = '#2ed573';
mZero.style.color = '#2ed573';
sZero.style.color = '#2ed573';
notification.innerText = 'Counting Down!';
if (startCountdown === undefined) {
startCountdown = setInterval(play, 1000);
} else {
notification.innerText = 'Timer is already running!';
}
if (stopBtn.style.display == 'inline') {
stopBtn.style.display = 'none';
pauseBtn.style.display = 'inline';
}
});
function reset() {
minutes.innerText = 25;
seconds.innerText = '00';
stopCountdown();
TimeUpMessage.classList.replace('show', 'hide');
minutes.classList.replace('hide', 'show');
seconds.classList.replace('hide', 'show');
startCountdown = undefined;
location.reload();
}
stopBtn.addEventListener('click', reset);
pauseBtn.addEventListener('click', function () {
notification.innerText = 'Paused!';
stopBtn.style.display = 'inline';
pauseBtn.style.display = 'none';
stopCountdown();
startCountdown = undefined;
});
arrowUp.addEventListener('click', function () {
if (startCountdown === undefined) {
minutes.innerText++;
sessionLength.innerText++;
}
});
arrowDown.addEventListener('click', function () {
if (startCountdown === undefined && minutes.innerText > 0) {
minutes.innerText--;
sessionLength.innerText--;
}
});
function play() {
if (seconds.innerText != 0) {
seconds.innerText--;
} else if (seconds.innerText == 0 && minutes.innerText != 0) {
seconds.innerText = 59;
minutes.innerText--;
}
if (seconds.innerText == 0 && minutes.innerText == 0) {
timeUp = true;
if (timeUp == true) {
sZero.style.display = 'none';
mZero.style.display = 'none';
}
minutes.classList.add('hide');
seconds.classList.add('hide');
colon.classList.replace('show', 'hide');
TimeUpMessage.classList.replace('hide', 'show');
notification.innerText = 'Click anywhere to start a new session!';
document.querySelector('html').addEventListener('click', reset);
}
if (seconds.innerText >= 0 && seconds.innerText < 10 && timeUp == false) {
sZero.style.display = 'inline';
} else {
sZero.style.display = 'none';
}
if (minutes.innerText >= 0 && minutes.innerText < 10 && timeUp == false) {
mZero.style.display = 'inline';
} else {
mZero.style.display = 'none';
}
}
function stopCountdown() {
clearInterval(startCountdown);
}