-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.js
201 lines (169 loc) · 5.77 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
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
/*!
* Timer provides a low-level loop solution
* it's use the requestAnimationFrame if supported, else fallback to a setInterval method (inspired by paul Irish polyfill)
* Timer got a mechanism to stop the loop when no need, and start when needed.
*
* 6 methods:
* --> control the loop by yourself if needed
* start(): launch loop
* stop(): stop loop
* toggle(): toggle loop state
*
* --> register/unregister callbacks to Timer
* add(callback, fps, duration, delay): add a 'callback' to be executed 'fps' (default 12) time a second for 'duration' (default Infinity) after 'delay' (default 0)
*
* This method return a TimerItem. If you want to control your callback (remove), assign it to a variable
*
* Timer.add(callback, 10) // will be executed 10 times by second until you remove it manually (see remove)
* Timer.add(callback, 10, 3) // will be executed 10 times by second for 3 seconds
* Timer.add(callback, 10, 3, 5) will be executed 10 times by second for 3 seconds after 5 seconds
*
* Timer.add(TimerItem) // will be hooked to the Timer again with same params
*
* return a TimerItem
*
* remove(TimerItem):remove the TimerItem from the callback list, stop all animation
*
* delay: add a callback to execute 1 time after x delay
* Timer.delay(callback, 10) callback will be execute after 10 seconds
*/
var Timer = (function () {
var Engine = (function (){
var interval = null;
var PAUSED = true
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
false
})();
return {
start: function (){
if(!PAUSED) return;
PAUSED = false;
if(!window.requestAnimFrame){
interval = setInterval(loop, 25);
}
else {
loop();
}
},
stop: function (){
PAUSED = true;
if(!window.requestAnimFrame){
clearInterval(interval)
}
},
loop: function (){
if(!PAUSED && window.requestAnimFrame) {
window.requestAnimFrame(loop);
}
},
isRunning: function (){
return !PAUSED;
}
}
})();
var TIME = 0,
CB = [];
function loop(time) {
TIME = time || (new Date()).getTime();
for (var i = 0, l = CB.length, o; i < l; i++) {
o = CB[i];
if (o && o.start <= TIME && (TIME - o.current >= o.interval)) {
// si o existe
// et si le startTime est inferieur à maintenant
// et si l'interval est respecte
if(o.end <= TIME){
o.callback(o.end, 1, o);
Timer.remove(o);
}
else {
o.callback(TIME, (TIME-o.start)/o.duration, o);
o.current = TIME;
}
}
}
if (Engine.isRunning() && CB.length) {
Engine.loop();
}
else if(Timer.autostart){
Timer.stop();
}
}
function getUid () {
return "" + (TIME || (new Date).getTime()) + "-" + parseInt(Math.random() * 10000);
}
return {
//start the loop
start: function () {
Engine.start();
},
//stop the loop
stop: function () {
Engine.stop();
},
// toggle loop state
toggle: function () {
(!Engine.isRunning()) ? Engine.start() : Engine.stop();
},
// add a function to the loop
add: function (fn, fps, duration, delay) {
if(arguments.length == 1 && typeof fn != "function"){
fps = fn.fps;
duration = fn.duration;
delay = fn.delay;
fn = fn.callback;
}
fps = (fps && fps > 100) ? 100 : (typeof fps == "undefined") ? 100 : fps;
duration = (duration) ? duration : Infinity;
delay = (delay) ? delay : 0;
var startTime = ((new Date).getTime()) + delay;
var o = {
callback: fn,
start: startTime ,
end: startTime + duration,
interval: 1000 / fps,
current: -Infinity,
fps: fps,
duration: duration,
delay: delay,
uid: getUid()
};
CB.push(o);
if(Timer.autostart && !Engine.isRunning()) {
Engine.start();
}
// return TimerItem object
return o;
},
// run a function every interval
each: function (fn, interval){
return Timer.add(fn, 1/(interval/1000), Infinity, 0);
},
// run a
delay: function (fn, duration){
return Timer.add(fn, 1, 1, duration);
},
_clear: function (){
CB = [];
},
_getCB: function (){
return CB;
},
remove: function (o){
for (var i = 0, cb, l = CB.length; i < l; i++) {
cb = CB[i];
if(cb && cb.uid == o.uid){
CB.splice(i, 1);
}
}
},
isRunning: function (){
return Engine.isRunning();
},
autostart: true
}
})();