-
Notifications
You must be signed in to change notification settings - Fork 5
/
Timer.js
203 lines (187 loc) · 5.93 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
202
203
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
// TODO fix func-style
/*eslint func-style: [0, "declaration"] */
define(function(require, exports, module) {
/**
* An internal library to reproduce javascript time-based scheduling.
* Using standard javascript setTimeout methods can have a negative performance impact
* when combined with the Famous rendering process, so instead require Timer and call
* Timer.setTimeout, Timer.setInterval, etc.
*
* @class Timer
* @constructor
*/
var FamousEngine = require('famous/core/Engine');
var _event = 'prerender';
var getTime = (window.performance) ?
function() {
return window.performance.now();
}
: function() {
return Date.now();
};
/**
* Add a function to be run on every prerender
*
* @method addTimerFunction
*
* @param {function} fn function to be run every prerender
*
* @return {function} function passed in as parameter
*/
function addTimerFunction(fn) {
FamousEngine.on(_event, fn);
return fn;
}
/**
* Wraps a function to be invoked after a certain amount of time.
* After a set duration has passed, it executes the function and
* removes it as a listener to 'prerender'.
*
* @method setTimeout
*
* @param {function} fn function to be run after a specified duration
* @param {number} duration milliseconds from now to execute the function
*
* @return {function} function passed in as parameter
*/
function setTimeout(fn, duration) {
var t = getTime();
var callback = function() {
var t2 = getTime();
if (t2 - t >= duration) {
fn.apply(this, arguments);
FamousEngine.removeListener(_event, callback);
}
};
return addTimerFunction(callback);
}
/**
* Wraps a function to be invoked after a certain amount of time.
* After a set duration has passed, it executes the function and
* resets the execution time.
*
* @method setInterval
*
* @param {function} fn function to be run after a specified duration
* @param {number} duration interval to execute function in milliseconds
*
* @return {function} function passed in as parameter
*/
function setInterval(fn, duration) {
var t = getTime();
var callback = function() {
var t2 = getTime();
if (t2 - t >= duration) {
fn.apply(this, arguments);
t = getTime();
}
};
return addTimerFunction(callback);
}
/**
* Wraps a function to be invoked after a certain amount of prerender ticks.
* Similar use to setTimeout but tied to the engine's run speed.
*
* @method after
*
* @param {function} fn function to be run after a specified amount of ticks
* @param {number} numTicks number of prerender frames to wait
*
* @return {function} function passed in as parameter
*/
function after(fn, numTicks) {
if (numTicks === undefined) return undefined;
var callback = function() {
numTicks--;
if (numTicks <= 0) { //in case numTicks is fraction or negative
fn.apply(this, arguments);
clear(callback);
}
};
return addTimerFunction(callback);
}
/**
* Wraps a function to be continually invoked after a certain amount of prerender ticks.
* Similar use to setInterval but tied to the engine's run speed.
*
* @method every
*
* @param {function} fn function to be run after a specified amount of ticks
* @param {number} numTicks number of prerender frames to wait
*
* @return {function} function passed in as parameter
*/
function every(fn, numTicks) {
numTicks = numTicks || 1;
var initial = numTicks;
var callback = function() {
numTicks--;
if (numTicks <= 0) { //in case numTicks is fraction or negative
fn.apply(this, arguments);
numTicks = initial;
}
};
return addTimerFunction(callback);
}
/**
* Remove a function that gets called every prerender
*
* @method clear
*
* @param {function} fn event linstener
*/
function clear(fn) {
FamousEngine.removeListener(_event, fn);
}
/**
* Executes a function after a certain amount of time. Makes sure
* the function is not run multiple times.
*
* @method debounce
*
* @param {function} func function to run after certain amount of time
* @param {number} wait amount of time
*
* @return {function} function that is not able to debounce
*/
function debounce(func, wait) {
var timeout;
var ctx;
var timestamp;
var result;
var args;
return function() {
ctx = this;
args = arguments;
timestamp = getTime();
var fn = function() {
var last = getTime - timestamp;
if (last < wait) {
timeout = setTimeout(fn, wait - last);
} else {
timeout = null;
result = func.apply(ctx, args);
}
};
clear(timeout);
timeout = setTimeout(fn, wait);
return result;
};
}
module.exports = {
setTimeout : setTimeout,
setInterval : setInterval,
debounce : debounce,
after : after,
every : every,
clear : clear
};
});