-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatchr.js
103 lines (97 loc) · 2.93 KB
/
stopwatchr.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
/* Logic */
function getCurrentTime() {
return parseInt(entered.join(""));
}
// Expect it to countdown clock
function startCountdown() {
// Save starting value
options.startvalue = entered;
// If starting from 1 have countdown start early
updateCurrentTime(getCurrentTime() + 1);
// Add one second so that it will countdown from the second prior
// entered[3] = entered[3] + 1;
timer = setInterval(function () {
// console.log("running");
var countdown = updateClock();
if (countdown <= 1) {
stopCountdown();
// If paused reset clock and start
if (options.repeat) {
repeatClock();
}
}
}, 1000);
};
function repeatClock() {
setTimeout(function () {
fireNotifier("repeat");
resetClock();
startCountdown();
}, 1000);
}
function fireIntervalSound(intervalperiod, currentvalue) {
// -1 fires it upon arriving at that value
var remainder = (currentvalue - 1) % intervalperiod;
remainder = Math.round(remainder);
if (remainder == 0) {
audio.play();
fireNotifier("interval");
}
};
function updateCurrentTime(value) {
newvalue = pad(value, 4);
entered = newvalue.split("");
return entered;
}
// Expect it to countdown from current clock
function updateClock() {
var currentValue = getCurrentTime();
fireIntervalSound(intervalperiod, currentValue);
// Dont let it count at 0 or will goto -1
if (currentValue > 0) {
var digitsarray = updateCurrentTime(currentValue - 1);
updateDigits(digitsarray);
// Let it cycle at 1 to count to 0
if (currentValue > 1) {
cycleMs();
}
}
return currentValue;
}
// Expect it to pause countdown
function pauseCountdown() {
fireNotifier();
clearInterval(timer);
clearInterval(mstimer);
};
// Expect it to pause and reset countdown
function stopCountdown() {
fireNotifier();
clearInterval(timer);
clearInterval(mstimer);
timer = null;
mstimer = null;
}
function cycleMs() {
clearInterval(mstimer);
var safeguard = 0;
var count = 99;
mstimer = setInterval(function () {
safeguard++;
count = count - 1;
updateMs(count);
if (count <= 0 || safeguard == 110) {
resetMs();
};
}, 10);
};
function resetMs() {
$(mscounter).html("00");
clearInterval(mstimer);
mstimer = null;
};
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}