-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.js
66 lines (51 loc) · 1.32 KB
/
timer.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
var endtime = null;
var stepper = null;
var defaulttime = (2 * 60 + 30) * 1000;
function displayTime(d, func) {
var min = 0, sec = 0, ms = 0;
if (d > 0) {
var dd = d;
ms = Math.floor(d % 1000 / 100);
d = Math.floor(d / 1000);
sec = d % 60;
d = Math.floor(d / 60);
min = d;
d = dd;
}
else {
stopTimer();
}
$('#time #min').text(min);
$('#time #sec').text((sec < 10 ? '0' : '') + sec);
$('#time #ms').text(ms);
(func || function(){})(d);
}
function startTimer(func) {
$("#timer").text("Stop").removeClass("btn-default btn-success").addClass("btn-danger");
endtime = Date.now() + defaulttime;
stepper = setInterval(function() {
displayTime(endtime - Date.now(), func);
}, 100);
gtag('event', 'timer', {
'type': 'start'
});
}
function stopTimer(reset) {
$("#timer").text("Start").removeClass("btn-default btn-danger").addClass("btn-success");
endtime = null;
clearInterval(stepper);
stepper = null;
if (!reset) {
gtag('event', 'timer', {
'type': 'stop',
'time': $("#time").text().trim()
});
}
}
function resetTimer(func) {
displayTime(defaulttime, func);
stopTimer(true);
}
function isTimer() {
return endtime !== null;
}