-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathscript.js
109 lines (96 loc) · 3.01 KB
/
script.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
class PomodoroTimer
{
constructor(workDuration, breakDuration)
{
this.workDuration = workDuration * 60;
this.breakDuration = breakDuration * 60;
this.timer = null;
this.isWorking = true;
this.workAudio = new Audio("work.mp3");
this.breakAudio = new Audio("break.mp3");
this.workAudio.loop = true;
this.breakAudio.loop = true;
}
startTimer()
{
this.timer = setInterval(() =>
{
this.updateTimer();
}, 1000);
if(this.isWorking)
{
this.workAudio.play();
this.breakAudio.pause();
}
else
{
this.breakAudio.play();
this.workAudio.pause();
}
}
stopTimer()
{
clearInterval(this.timer);
this.workAudio.pause();
this.breakAudio.pause();
}
updateTimer()
{
const timeDisplay = document.getElementById("time-display");
if(this.isWorking)
{
this.workDuration--;
}
else
{
this.breakDuration--;
}
if(this.workDuration === 0 && this.isWorking)
{
this.stopTimer();
this.isWorking = false;
alert("Time for a break!");
this.startTimer();
}
else if(this.breakDuration === 0 && !this.isWorking)
{
this.stopTimer();
this.isWorking = true;
alert("Back to work!");
this.startTimer();
}
const minutes = Math.floor(this.isWorking ? this.workDuration / 60 : this.breakDuration / 60);
const seconds = this.isWorking ? this.workDuration % 60 : this.breakDuration % 60;
const displayMinutes = minutes < 10 ? "0" + minutes : minutes;
const displaySeconds = seconds < 10 ? "0" + seconds : seconds;
timeDisplay.innerText = `${displayMinutes}:${displaySeconds}`;
}
resetTimer()
{
this.stopTimer();
this.workDuration = document.getElementById("work-duration").value * 60;
this.breakDuration = document.getElementById("break-duration").value * 60;
this.isWorking = true;
const timeDisplay = document.getElementById("time-display");
const displayMinutes = this.workDuration < 10 ? "0" + this.workDuration : this.workDuration;
timeDisplay.innerText = `${displayMinutes}:00`;
}
}
const startButton = document.getElementById("start-button");
const resetButton = document.getElementById("reset-button");
const timer = new PomodoroTimer(
document.getElementById("work-duration").value,
document.getElementById("break-duration").value
);
startButton.addEventListener("click", () =>
{
timer.startTimer();
startButton.disabled = true;
resetButton.disabled = false;
});
resetButton.addEventListener("click", () =>
{
timer.resetTimer();
startButton.disabled = false;
resetButton.disabled = true;
});