-
Notifications
You must be signed in to change notification settings - Fork 2
/
pomodoro.js
64 lines (50 loc) · 1.63 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
/*jslint esversion: 6 */
let timerEventId;
function publishTimer(timer) {
"use strict";
const timerElement = document.getElementById("timerText");
timerElement.innerHTML = timer;
document.title = "[" + timer + "] Pomodoro Timer";
}
function stopTimer() {
const alarmElement = document.getElementById("alarm");
clearInterval(timerEventId);
alarmElement.pause();
publishTimer('00:00');
};
function countdown(interval) {
"use strict";
const alarmElement = document.getElementById("alarm");
let endTime;
function formatTimeSegment(segment) {
if (segment.toString().length < 2) {
segment = "0" + segment;
}
return segment;
};
function setTimer() {
let formattedTime, timeLeft, hours, minutes, seconds;
timeLeft = new Date(endTime - Date.now());
if (timeLeft.getTime() < 1000) {
formattedTime = "00:00";
publishTimer(formattedTime);
clearInterval(timerEventId);
alarmElement.currentTime = 0;
alarmElement.play();
} else {
hours = timeLeft.getUTCHours();
minutes = formatTimeSegment(timeLeft.getUTCMinutes());
seconds = formatTimeSegment(timeLeft.getUTCSeconds());
formattedTime = (hours ? hours + ":" + minutes : minutes) + ":" + seconds;
publishTimer(formattedTime);
}
};
clearInterval(timerEventId);
alarmElement.pause();
endTime = Date.now() + (1000 * interval) + 1000;
timerEventId = setInterval(setTimer, 100);
};
window.onload = function () {
"use strict";
stopTimer();
};