-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomodoro.js
112 lines (105 loc) · 2.89 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
106
107
108
109
110
111
112
$(document).ready(function() {
let timeRunning = false;
let timeLeft;
let sessionTime = 1500;
let breakTime = 300;
let breakClock = false;
let seconds = sessionTime;
function play1(){
let audio = document.getElementById("audio1");
audio.play();
}
function play2(){
let audio = document.getElementById("audio2");
audio.play();
}
//Converts seconds to MM:SS
function filterTime(seconds) {
let min = Math.floor(seconds / 60);
let sec = seconds % 60;
if(sec < 10) {
return min + ":0" + sec;
} else {
return min + ":" + sec;
}
}
function timer() {
if(seconds > 0) {
seconds-- ;
if(seconds === 0) {
if($('.session').html() === "Break:"){
play1();
} else {
play2();
}
}
} else {
if(!breakClock) {
seconds = breakTime;
$('.session').html("Break:");
breakClock = true;
} else {
seconds = sessionTime;
$('.session').html("Session:");
breakClock = false;
}
}
$('#time').html(filterTime(seconds));
}
//Initial page render
$('#time').html(filterTime(seconds));
$('#sessionLength').html(Math.floor(seconds / 60));
$('#breakLength').html(Math.floor(breakTime / 60));
//add play/pause functionality to the stopwatch button
$('#start, #resume').click(function() {
play1();
$(".plus-minus").attr("disabled", true);
$(this).hide();
$("#stop").show();
$("#stop").on("click", function(){
$(".plus-minus").attr("disabled", false);
$(this).hide();
$("#resume").show();
clearInterval(timeLeft);
timeRunning = false;
});
if(!timeRunning) {
timeLeft = setInterval(function() {timer()}, 1000);
timeRunning = true;
} else {
clearInterval(timeLeft);
timeRunning = false;
}
});
//reset button brings app back to session mode at full time.
$('#reset').click(function() {
$(".plus-minus").attr("disabled", false);
$("#stop").hide();
$("#resume").hide();
$("#start").show();
seconds = sessionTime;
$('.session').html("Session:");
$('#time').html(filterTime(seconds));
breakClock = false;
clearInterval(timeLeft);
timeRunning = false;
});
$(".plus-minus").click(function(e){
let method = e.target.getAttribute("data-method");
switch(method) {
case "add-sess": sessionTime += 60;
break;
case "sub-sess": sessionTime = sessionTime <= 60 ? 0 :sessionTime - 60;
break;
case "add-break": breakTime += 60;
break;
case "sub-break": breakTime = breakTime <= 60 ? 0 : breakTime - 60;
break;
}
//sets seconds to the appropriate value
seconds = breakClock ? breakTime : sessionTime;
$('#time').html(filterTime(seconds));
$('#sessionLength').html(Math.floor(sessionTime/60));
$('#breakLength').html(Math.floor(breakTime/60));
});
});