-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
268 lines (234 loc) · 8.21 KB
/
Copy pathscript.js
File metadata and controls
268 lines (234 loc) · 8.21 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// create audio elements ------------------------------------------
var STARTSOUND = document.createElement("audio");
var STOPSOUND = document.createElement("audio");
// timer sounds ---------------------------------
var TOBREAK = document.createElement("audio");
var TOREST = document.createElement("audio");
var TOSESSION = document.createElement("audio");
// reset sounds -----------------------------
var RESETLAP = document.createElement("audio");
// sample loading -------------------------------------------------
TOBREAK.setAttribute("src","./sfx/to-break.ogg");
TOBREAK.setAttribute("preload", "auto");
TOREST.setAttribute("src","./sfx/to-rest.ogg");
TOREST.setAttribute("preload", "auto");
TOSESSION.setAttribute("src","./sfx/to-session.ogg");
TOSESSION.setAttribute("preload", "auto");
RESETLAP.setAttribute("src","./sfx/reset-lap.ogg");
RESETLAP.setAttribute("preload", "auto");
STARTSOUND.setAttribute("src","./sfx/start-clock.ogg");
STARTSOUND.setAttribute("preload","auto");
STOPSOUND.setAttribute("src","./sfx/stop-clock.ogg");
STOPSOUND.setAttribute("preload","auto");
var breakTime = 5 * 60; //break timer, in seconds. defaults to 5 minutes.
var sessionTime = 25 * 60; //session timer, in seconds. defaults to 25 minutes.
var restTime = 15 * 60; //rest time, in seconds. defaults to 15 minutes.
var i = 0; //current clock timer, in seconds
var lap = 0; // keeps track of how many laps have eLAPSed
var laps = ["I","II","III","IV","V"];
var running = 0; // boolean for keeping track of clock state
var muted = 1; // determines whether sound should play or not
var currentTimer = 'session'; //keeps track of which countdown is currently active - session / break / rest
var timeout = null; //timeout storage
$(document).ready(function(){
//set current timer equal to the default session time length
i = sessionTime;
//creates minutes/seconds from total time in seconds, and displays them to the user
//updateTime returns an array with the time, in the order of "seconds, minutes"
var sessionTimeObj = updateTime(sessionTime);
var breakTimeObj = updateTime(breakTime);
var restTimeObj = updateTime(restTime);
var currentTimeObj = updateTime(i);
//updates all time displays with the newly created second/minute objects.
updateBreakDisplay(breakTimeObj.minutes);
updateSessionDisplay(sessionTimeObj.minutes);
updateRestDisplay(restTimeObj.minutes);
updateTimeDisplay(currentTimeObj);
});
function mainLoop(){
// behavior when timer zeroes out
// increments lap counter, determines timer type and specifies timer sounds to play
if (i <= 0){
if(currentTimer === 'session'){
// increment laps after a session is over
lap++;
// change timer to break if laps are less than 4
if(lap < 4){
currentTimer = 'break';
i = breakTime;
playSound(TOBREAK);
// change timer to rest if laps are 4 or more
}else if(lap >= 4){
currentTimer = 'rest';
i = restTime;
playSound(TOREST);
}
}else if(currentTimer === 'break' || currentTimer === 'rest'){
if(currentTimer === 'rest'){
// resets laps after a rest period
lap = 0;
}
currentTimer = 'session';
i = sessionTime;
playSound(TOSESSION);
}
updateLapDisplay(lap);
}
//updates every second
if(running === 1 && i > 0){
i--;
currentTimeObj = updateTime(i);
updateTimeDisplay(currentTimeObj);
//stores the timeout in a referral var
timeout = setTimeout(mainLoop, 1000);
}
}
function toggleRunning(){
//toggles running between 0 and 1
//uses XOR gate; outputs 1 if only one input is 1
running ^= 1;
}
function toggleSound(){
// see toggleRunning
muted ^= 1;
}
function playSound(sound){
if(muted === 0){
sound.currentTime = 0;
sound.play();
}
}
function interruptTimeout(timeout){
//interrupts the timeout if set
if(timeout !== null){
clearTimeout(timeout);
timeout = null;
}
}
function updateTimeDisplay(obj){
//flushes the display area and appends the updated data
var secondDisplay = $('.seconds');
var minuteDisplay = $('.minutes');
secondDisplay.empty().append(padZeroes(obj.seconds,2));
minuteDisplay.empty().append(obj.minutes);
//displays remaining time in page title
$(document).attr('title',currentTimer.toUpperCase()+': '+obj.minutes+":"+padZeroes(obj.seconds,2)+" // POMODORO");
}
function updateTime(i){
//creates seconds and minutes from supplied time, in seconds, and returns an object
var seconds = Math.floor(i % 60);
var minutes = Math.floor(i / 60);
var timeObj = {
seconds: seconds,
minutes: minutes
};
return timeObj;
}
function updateSessionDisplay(m){
//flushes session length display and appends updated data
$('.session-length').empty();
$('.session-length').append(m);
}
function updateBreakDisplay(m){
//flushes session length display and appends updated data
$('.break-length').empty();
$('.break-length').append(m);
}
function updateRestDisplay(m){
$('.rest-length').empty();
$('.rest-length').append(m);
}
function padZeroes(num,size){
//adds specified amount of 0s in front of number input, then removes the 0s if the amount of digits is larger than 0s
return ('00'+num).substr(-size);
}
function timerLengthChange(timeUnit, operator){
//adds or subtracts 60 seconds to the session time, depending on the data-set value, the operator
if(operator === 'inc'){
timeUnit += 60;
}else if(operator === 'dec' && timeUnit > 0){
timeUnit -= 60;
}
return timeUnit;
}
function timerChangeStopClock(timerType, currentTimer, timeout, timeUnit, i){
if(currentTimer === timerType){
//stop current timer
$('.startstop').empty().append("START");
running = 0;
interruptTimeout(timeout);
// set timer
//set total time to new session amount
currentTimeObj = updateTime(timeUnit)
updateTimeDisplay(currentTimeObj);
return timeUnit;
}
return i;
}
function updateLapDisplay(lap){
$('.lap-counter').empty().append(laps[lap]);
}
$('.startstop').on('click', function(){
//interrupts current count loop if counter is running on click
toggleRunning();
interruptTimeout(timeout);
mainLoop();
if(running === 1){
playSound(STARTSOUND);
$(this).empty().append("STOP");
}else if(running === 0){
playSound(STOPSOUND);
$(this).empty().append("START");
}
});
$('.reset').on('click',function(){
// resets the currently running timer back to SESSION, with the specified session timer length
if(currentTimer === 'rest'){
lap = 0;
updateLapDisplay(lap);
}
playSound(RESETLAP);
interruptTimeout(timeout);
i = sessionTime;
currentTimer = 'session';
currentTimeObj = updateTime(i);
updateTimeDisplay(currentTimeObj);
mainLoop();
});
$('.session-change').on('click',function(){
//adds or subtracts 60 seconds to the session time, depending on the data-set value, the operator
var operator = $(this).attr('data-set');
sessionTime = timerLengthChange(sessionTime, operator);
// stops clock when the current session timer is changed
i = timerChangeStopClock('session', currentTimer, timeout, sessionTime, i);
//update the session time object and display the new data
sessionTimeObj = updateTime(sessionTime);
updateSessionDisplay(sessionTimeObj.minutes);
});
$('.break-change').on('click', function(){
var operator = $(this).attr('data-set');
breakTime = timerLengthChange(breakTime, operator);
// stops clock when the current session timer is changed
i = timerChangeStopClock('break', currentTimer, timeout, breakTime, i);
//update the break time object and display the new data
breakTimeObj = updateTime(breakTime);
updateBreakDisplay(breakTimeObj.minutes);
});
$('.rest-change').on('click', function(){
var operator = $(this).attr('data-set');
restTime = timerLengthChange(restTime, operator);
i = timerChangeStopClock('rest', currentTimer, timeout, restTime, i);
restTimeObj = updateTime(restTime);
updateRestDisplay(restTimeObj.minutes);
});
$('.junq').on('click',function(){
window.open('https://github.com/junkdeck/','_blank');
});
$('.sounder').on('click', function(){
toggleSound();
if(muted === 1){
$('.sounder-waves').css("opacity","0");
}else if(muted === 0){
$('.sounder-waves').css("opacity","1");
}
});