-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimer.js
More file actions
146 lines (116 loc) · 3.51 KB
/
timer.js
File metadata and controls
146 lines (116 loc) · 3.51 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
var $timerSetupForm = document.querySelector("#timer-setup");
var $countdownContainer = document.querySelector("#countdown");
var $countdown = document.querySelector(".display");
var $cancelCountdown = document.querySelector(".cancel");
var minutes = 15;
var seconds = 0;
var upwards = true;
var countDown;
var blinker;
$timerSetupForm.onsubmit = function() {
minutes = Number(document.querySelector("#minutes").value);
seconds = Number(document.querySelector("#seconds").value);
direction = document.querySelector('input[name="direction"]:checked').value;
if (direction == "up") {
upwards = true;
} else {
upwards = false;
$countdown.innerHTML = zeroPad(minutes, 2) + ":" + zeroPad(seconds, 2);
}
countDown = beginCountdown(minutes, seconds);
return false;
}
$cancelCountdown.onclick = cancelCountdown;
function beginCountdown(minutes, seconds) {
$timerSetupForm.style.left = 10000;
$countdownContainer.style.left = 0;
var totalSeconds = (60 * minutes) + seconds;
var secondsRemaining = totalSeconds;
var secondsSinceEnd = 0 - totalSeconds;
var initialMinutes = minutes;
var initialSeconds = seconds;
var secondsElapsed = 0;
var warned = false;
return setInterval(function() {
secondsElapsed++;
secondsRemaining--;
secondsSinceEnd++;
if (upwards) {
minutes = Math.floor(secondsElapsed / 60);
seconds = secondsElapsed - (minutes * 60);
} else {
if (secondsRemaining >= 0){
minutes = Math.floor(secondsRemaining / 60);
seconds = secondsRemaining - (minutes * 60);
} else {
minutes = Math.floor(secondsSinceEnd / 60);
seconds = secondsSinceEnd - (minutes * 60);
}
}
//if (upwards) {
$countdown.innerHTML = zeroPad(minutes, 2) + ":" + zeroPad(seconds, 2);
// } else {
// $countdown.innerHTML = zeroPad(initialMinutes-minutes-1, 2) + ":" + zeroPad(initialSeconds-seconds+59, 2);
// }
$countdownContainer.style.backgroundColor = "rgba(255, 0, 0, " + (secondsElapsed / totalSeconds) * (secondsElapsed / totalSeconds) + ")"
if (secondsElapsed / totalSeconds >= 0.8 && !warned) {
beep(200, 1);
warned = true;
}
if (secondsElapsed >= totalSeconds) {
beep(200, 2);
done = true;
if (!blinker) blinker = blink($countdown, 400);
// cancelCountdown();
}
}, 1000);
}
function cancelCountdown() {
clearInterval(countDown);
clearInterval(blinker);
countDown = null;
blinker = null;
$countdown.style.display = "";
$countdownContainer.style.backgroundColor = null;
$timerSetupForm.style.left = 0;
$countdownContainer.style.left = -10000;
$countdown.innerHTML = "00:00";
}
function zeroPad(number, numLength) {
numStr = number.toString();
while (numStr.length != numLength) {
numStr = "0" + numStr;
}
return numStr;
}
function blink(element, speed) {
var defaultDisplay = element.style.display;
speed = speed || 200;
return setInterval(function() {
element.style.display = element.style.display != "none" ? "none" : defaultDisplay;
}, speed)
}
var beep = (function () {
var webaudio = window.audioContext || window.webkitAudioContext;
if (webaudio) {
var ctx = new webaudio;
return function (duration, type, finishedCallback) {
duration = +duration;
// Only 0-4 are valid types.
type = (type % 5) || 0;
if (typeof finishedCallback != "function") {
finishedCallback = function () {};
}
var osc = ctx.createOscillator();
osc.type = type;
osc.connect(ctx.destination);
osc.noteOn(0);
setTimeout(function () {
osc.noteOff(0);
finishedCallback();
}, duration);
};
} else {
return function() {};
}
})();