-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomodoro.js
105 lines (87 loc) · 3.52 KB
/
pomodoro.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
// pomodoro session times in minutes
const pSessionTime = 25;
const sBreakLength = 5;
const lBreakLength = 30;
// counter to count the number of short breaks
var counter = 0;
document.getElementById("demo").innerHTML = pSessionTime + "m : 00s";
function startPomodoro(){
// Set the date we're counting down to
var countDownDate = new Date();
countDownDate.setTime(countDownDate.getTime() + pSessionTime * 60 * 1000);
var countDownTime = countDownDate.getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownTime - now;
// Time calculations for days, hours, minutes and seconds
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = minutes + "m : " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
if(counter < 3) {
let notification = new Notification('Short Break Time!');
startShortBreak();
counter++;
}
else {
let notification = new Notification('Long Break Time!');
startLongBreak();
counter = 0;
}
}
}, 1000);
}
function startShortBreak(){
// Set the date we're counting down to
var countDownDate = new Date();
countDownDate.setTime(countDownDate.getTime() + sBreakLength * 60 * 1000);
var countDownTime = countDownDate.getTime();
// Update the count down every 1 second
var y = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownTime - now;
// Time calculations for days, hours, minutes and seconds
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = minutes + "m : " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(y);
let notification = new Notification('Break Time Over!');
startPomodoro();
}
}, 1000);
}
function startLongBreak() {
// Set the date we're counting down to
var countDownDate = new Date();
countDownDate.setTime(countDownDate.getTime() + sBreakLength * 60 * 1000);
var countDownTime = countDownDate.getTime();
// Update the count down every 1 second
var z = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownTime - now;
// Time calculations for days, hours, minutes and seconds
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = minutes + "m : " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(z);
let notification = new Notification('Break Time Over!');
startPomodoro();
}
}, 1000);
}