This repository has been archived by the owner on Nov 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventTimer.js
331 lines (285 loc) · 10.1 KB
/
eventTimer.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*global window: false */
/*global console: false */
var eventTimer = function () {
'use strict';
var startTime = 0,
now = 0,
eventIDList = [],
eventID = 0,
timerInitialised = false,
frameMean = 0,
frame = 1000 / 60,
eventList = [];
function stop() {
// force stop timer //
window.cancelAnimationFrame(window.requestAnimId);
// clear queue and reset timer settings //
startTime = 0;
now = 0;
eventIDList = [];
eventID = 0;
timerInitialised = false;
frameMean = 0;
frame = 1000 / 60;
eventList = [];
}
function start() {
if (!timerInitialised) {
timerInitialised = true;
var lastFrameTime = 0,
frameTimeHistory = [],
frameTimeHistoryLength = 60,
first = true,
eventFun = function (animationFrameTimeStamp) {
// Update timestamps //
// get current frame timestamp
now = animationFrameTimeStamp;
// if it's the first loop, set the start time
if (first) {
startTime = now;
first = false;
}
// get the timestamp from the last loop
if (lastFrameTime === 0) {
lastFrameTime = now;
}
// calculate the interval between now and last animationFrame requests
var interval = now - lastFrameTime;
lastFrameTime = now;
///////////////////////
// create a rolling history of animationFrame interval times from the last 60 loops //
// if < 60 loops use 1000/60 as default
frameTimeHistory.unshift(interval);
while (frameTimeHistory.length > frameTimeHistoryLength) {
frameTimeHistory.pop();
}
if (frameTimeHistory.length === frameTimeHistoryLength) {
var total = 0;
for (var n = 0; n < frameTimeHistory.length; n++) {
total += frameTimeHistory[n];
}
frameMean = total / frameTimeHistory.length;
}
// console.log(frameMean);
////////////////////////
// process all events in queue //
var i = 0;
var numEvents = eventIDList.length;
var eventsToRemove = [];
while (i < numEvents && i < eventIDList.length) {
// update time remaining
eventIDList[i].timeRemaining -= interval;
// find events that are past due (timeRemaining <= 0) or within half the frame interval time
if (eventIDList[i].timeRemaining <= 0 || (frameMean > 0 && eventIDList[i].timeRemaining <= (frameMean / 2))) {
// console.log(eventIDList[i].timeRemaining)
// if setTimeout then remove / if setInterval then reset remaining time //
if (eventIDList[i].loop === false) {
eventsToRemove.push(eventIDList[i].id);
} else {
eventIDList[i].timeRemaining = eventIDList[i].waitTime;
}
// fire event function
eventIDList[i].eventFun();
}
i++;
}
// remove all expired events
for (var m = 0; m < eventsToRemove.length; m++) {
cancelRequest(eventsToRemove[m]);
}
// if all events have expired, then stop timer
if (eventIDList.length === 0) {
stop();
} else {
window.requestAnimId = window.requestAnimationFrame(eventFun);
}
};
window.requestAnimId = window.requestAnimationFrame(eventFun);
}
}
function setTimeout(fun, wait) {
if (!fun || !wait) {
return;
//return console.log("error: missing parameter");
}
if (typeof fun === 'function') {
eventID++;
var t = {
eventFun: fun,
timeRemaining: wait,
id: eventID,
waitTime: wait,
loop: false
};
eventIDList.push(t);
eventList.push(t);
start();
return eventID;
} else {
return;
//return console.log("error: not a function");
}
}
function setInterval(fun, wait) {
if (!fun || !wait) {
return;
//return console.log("error: missing parameter");
}
if (typeof fun === 'function') {
eventID++;
var t = {
eventFun: fun,
timeRemaining: wait,
id: eventID,
waitTime: wait,
loop: true
};
eventIDList.push(t);
eventList.push(t);
start();
return eventID;
} else {
return;
//return console.log("error: not a function");
}
}
// cancel setTimeout or setIntervals
// can pass an array of ids to cancel multiple events
function cancelRequest(id) {
if (Object.prototype.toString.call(id) === '[object Array]') {
for (var n = 0; n < id.length; n++) {
for (var i = 0; i < eventIDList.length; i++) {
if (eventIDList[i].id == id[n]) {
eventIDList.splice(i, 1);
eventList.splice(i, 1);
}
}
}
} else {
for (var m = 0; m < eventIDList.length; m++) {
if (eventIDList[m].id == id) {
eventIDList.splice(m, 1);
eventList.splice(m, 1);
}
}
}
}
function cancelAllRequests() {
eventIDList = [];
eventList = [];
}
// Set multiple timed events at once //
// Usage:
// eventTimer.setMultipleTO(
// [
// [event1, 1000],
// [event2, 2000],
// [event3, 3000]
// ]
// )
function setMultipleTO(array) {
var idArray = [];
for (var i = 0; i < array.length; i++) {
eventID++;
idArray.push(eventID);
var t = {
eventFun: array[i][0],
timeRemaining: array[i][1],
id: eventID,
waitTime: array[i][1],
loop: false
};
eventIDList.push(t);
eventList.push(t);
}
start();
return idArray;
}
function setMultipleInt(array) {
var idArray = [];
for (var i = 0; i < array.length; i++) {
eventID++;
idArray.push(eventID);
var t = {
eventFun: array[i][0],
timeRemaining: array[i][1],
id: eventID,
waitTime: array[i][1],
loop: true
};
eventIDList.push(t);
eventList.push(t);
}
start();
return idArray;
}
return {
"start": start,
"stop": stop,
"setTimeout": setTimeout,
"setInterval": setInterval,
"cancelRequest": cancelRequest,
"cancelAllRequests": cancelAllRequests,
"eventList": eventList,
"setMultipleTO": setMultipleTO,
"setMultipleInt": setMultipleInt
};
}();
// requestAnim shim layer by Paul Irish
window.requestAnimationFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback, element) {
// should '60' here be framerate?
window.setTimeout(callback, 1000 / 60);
};
})();
// use window.performance() to get max fast and accurate time in milliseconds
window.performance = window.performance || {};
window.performance.now = (function () {
var load_date = Date.now();
return window.performance.now ||
window.performance.mozNow ||
window.performance.msNow ||
window.performance.oNow ||
window.performance.webkitNow ||
function () {
return Date.now() - load_date;
};
})();
/*
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/
// requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame =
window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] ||
window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function()
{ callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
}());
*/