forked from bfattori/timersjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timers_func.html
235 lines (210 loc) · 7.32 KB
/
timers_func.html
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
<!DOCTYPE html>
<html>
<head>
<title>Functional Timer Testing</title>
<script type="text/javascript" src="timers.js"></script>
<script type="text/javascript">
var timer, repeater, multi, oneshot, trigger;
TimersJS.repeater(100, function() {
document.getElementById("poolSize").innerHTML = TimersJS.poolSize();
});
function addClass(el, className) {
if (el.className.indexOf(className) == -1) {
el.className += " " + className;
}
}
function removeClass(el, className) {
var s = el.className.indexOf(className);
var newClassName = el.className.substring(0, s);
newClassName += el.className.substr(s + className.length);
el.className = newClassName;
}
function hasClass(el, className) {
return (el.className.indexOf(className) != -1);
}
function startTimer() {
if (!timer) {
timer = TimersJS.timer(500, function() {
var el = document.querySelector("div.timer");
if (hasClass(el, "on")) {
removeClass(el, "on");
} else {
addClass(el, "on");
}
});
}
}
function restartTimer() {
if (timer) {
timer.restart();
}
}
function startInterval() {
if (!repeater) {
repeater = TimersJS.repeater(500, function(now, delta) {
var el = document.querySelector("div.repeater.led"),
del = document.querySelector("span.repeater.delta");
del.innerHTML = delta + "ms";
if (hasClass(el, "on")) {
removeClass(el, "on");
} else {
addClass(el, "on");
}
});
}
}
function stopInterval() {
repeater = repeater.kill();
}
function startMultiTimer() {
if (!multi) {
addClass(document.querySelector("div.multi"), "on");
multi = TimersJS.multi(500, 10, function(repetition) {
var el = document.querySelector("div.multi");
if (hasClass(el, "red")) {
removeClass(el, "red");
addClass(el, "green");
} else {
removeClass(el, "green");
addClass(el, "red");
}
}, function() {
removeClass(document.querySelector("div.multi"), "on");
multi = null;
});
}
}
function startOneShot() {
if (!oneshot) {
oneshot = TimersJS.oneShot(250, function() {
var el = document.querySelector("div.oneshot");
if (hasClass(el, "red")) {
removeClass(el, "red");
addClass(el, "green");
} else {
removeClass(el, "green");
addClass(el, "red");
}
oneshot = null;
});
}
}
function startTrigger() {
if (!trigger) {
var el = document.querySelector("div.trigger1");
addClass(el, "on");
trigger = TimersJS.trigger(5000, function() {
var el = document.querySelector("div.trigger1");
var el2 = document.querySelector("div.trigger2");
removeClass(el, "on");
removeClass(el2, "on");
trigger = null;
}, 500, function() {
var el = document.querySelector("div.trigger2");
if (hasClass(el, "on")) {
removeClass(el, "on");
} else {
addClass(el, "on");
}
});
}
}
function somethingCool() {
TimersJS.timer(250, function() {
var els = document.querySelectorAll("div.led"), i = 0;
while (i < els.length && hasClass(els.item(i), "on")) {
i++;
}
if (i == els.length) {
this.kill();
} else {
addClass(els.item(i), "on");
this.restart();
}
});
}
function pauseAll() {
TimersJS.pauseAllTimers();
}
function restartAll() {
TimersJS.restartAllTimers();
}
function cancelAll() {
TimersJS.cancelAllTimers();
}
function killAll() {
TimersJS.cancelAllTimers();
timer = null;
repeater = null;
multi = null;
oneshot = null;
trigger = null;
}
</script>
<style type="text/css">
div.led {
display: inline-block;
border: 1px solid;
border-radius: 10px;
width: 10px;
height: 10px;
margin: 3px;
}
div.led.green {
background: #080;
}
div.led.red {
background: #800;
}
div.led.green.on {
background: #0f0;
}
div.led.red.on {
background: #f00;
}
</style>
</head>
<body>
<h1>TimersJS Functional Testing</h1>
Timers in pool <span id="poolSize"></span>
<table>
<tr>
<td>
Timer: <div class="timer led green"><!-- --></div>
</td>
</tr>
<tr>
<td>
Repeater: <div class="repeater led green"><!-- --></div><span class="repeater delta"></span>
</td>
</tr>
<tr>
<td>
Multi: <div class="multi led green"><!-- --></div>
</td>
</tr>
<tr>
<td>
OneShot: <div class="oneshot led green on"><!-- --></div>
</td>
</tr>
<tr>
<td>
Trigger: <div class="trigger1 led green"><!-- --></div><div class="trigger2 led red"><!-- --></div>
</td>
</tr>
</table>
<input type="button" value="Start Timer" onclick="startTimer()"/>
<input type="button" value="Restart Timer" onclick="restartTimer()"/>
<input type="button" value="Start Repeater" onclick="startInterval()"/>
<input type="button" value="Stop Repeater" onclick="stopInterval()"/>
<input type="button" value="Start MultiTimer" onclick="startMultiTimer()"/>
<input type="button" value="Start OneShot" onclick="startOneShot()"/>
<input type="button" value="Start Trigger" onclick="startTrigger()"/><br/>
<input type="button" value="Pause all timers" onclick="pauseAll()"/>
<input type="button" value="Restart all" onclick="restartAll()"/>
<input type="button" value="Cancel all" onclick="cancelAll()"/>
<input type="button" value="Kill all" onclick="killAll()"/><br/>
<input type="button" value="Do Fun" onclick="somethingCool()"/>
</body>
</html>