-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (79 loc) · 2.07 KB
/
script.js
File metadata and controls
93 lines (79 loc) · 2.07 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
let minutes = 25;
let seconds = 0;
let timer;
let isPaused = false;
function startTimer() {
if (isPaused) {
isPaused = false;
} else {
if (document.getElementById('goal').textContent === '') {
openModal();
}
minutes = 25;
seconds = 0;
}
updateTimer();
timer = setInterval(updateTimer, 1000);
}
function updateTimer() {
if (seconds === 0) {
if (minutes === 0) {
clearInterval(timer);
alert('Challenge completed!');
startBreak();
return;
}
minutes--;
seconds = 59;
} else {
seconds--;
}
document.getElementById('minutes').textContent = padTime(minutes);
document.getElementById('seconds').textContent = padTime(seconds);
}
function startBreak() {
minutes = 5;
seconds = 0;
updateTimer();
timer = setInterval(updateTimer, 1000);
}
function resetTimer() {
clearInterval(timer);
minutes = 25;
seconds = 0;
isPaused = true;
document.getElementById('minutes').textContent = padTime(minutes);
document.getElementById('seconds').textContent = padTime(seconds);
document.getElementById('goal').textContent = '';
openModal();
}
function pauseTimer() {
clearInterval(timer);
isPaused = true;
}
function padTime(time) {
return time.toString().padStart(2, '0');
}
function openModal() {
const modal = document.getElementById('modal');
modal.style.display = 'block';
const closeBtn = document.getElementsByClassName('close')[0];
const goalBtn = document.getElementById('goalBtn');
closeBtn.onclick = closeModal;
goalBtn.onclick = setGoal;
}
function closeModal() {
const modal = document.getElementById('modal');
modal.style.display = 'none';
}
function setGoal() {
const goalInput = document.getElementById('goalInput');
const goal = goalInput.value.trim();
if (goal) {
document.getElementById('goal').textContent = goal;
closeModal();
}
}
document.getElementById('startBtn').addEventListener('click', startTimer);
document.getElementById('resetBtn').addEventListener('click', resetTimer);
document.getElementById('breakBtn').addEventListener('click', pauseTimer);