-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (74 loc) · 2.43 KB
/
Copy pathscript.js
File metadata and controls
89 lines (74 loc) · 2.43 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
let workDuration = 1 * 60;
let shortRest = 0.5 * 60;
let longRest = 0.5 * 60;
let timeleft = workDuration;
let currentRounds = 0;
let maxRounds = 4;
let isWork = true;;
let timerInterval = null;
const startBtn = document.getElementById("start");
startBtn.textContent = "Start";
function updateDisplay(){
let minutes = Math.floor(timeleft / 60);
let seconds = timeleft % 60;
document.getElementById("timer").textContent =
`${minutes.toString().padStart(2,"0")}:${seconds.toString().padStart(2,"0")}`;
}
function startTimer(){
if(timerInterval) return;
timerInterval = setInterval(() => {
if(isWork) startBtn.textContent = "Working...";
if(timeleft > 0){
timeleft --;
updateDisplay();
} else { // Time over
clearInterval(timerInterval);
timerInterval = null;
if(isWork){
currentRounds ++;
if(currentRounds < maxRounds){
isWork = false;
timeleft = shortRest;
startBtn.textContent = "Short rest..";
startTimer();
alert("Take a short rest!")
} else { //Rounds over
if(currentRounds === 4){
currentRounds = 0;
maxRounds = 4;
isWork = true;
timeleft = workDuration;
startBtn.textContent = "Start";
alert("Pomodoro finished!");
return;
} else {
isWork = false;
timeLeft = longRest;
startBtn.textContent = "Long Rest...";
startTimer();
alert("Take a long rest!");
}
}
} else { // !isWork
isWork = true;
timeleft = workDuration;
startBtn.textContent = "Working..";
startTimer();
alert("Time to work!");
}
}
},1000);
}
function resetTimer(){
clearInterval(timerInterval);
timerInterval = null;
timeleft = workDuration;
startBtn.textContent = "Start";
updateDisplay();
}
function pauseTimer() {
startBtn.textContent = "Restart";
clearInterval(timerInterval);
timerInterval = null;
}
updateDisplay();