-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwait-for-it.js
48 lines (40 loc) · 953 Bytes
/
wait-for-it.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
(function() {
'use strict';
var waitForIt = {
setup: function(func, wait, thisArg) {
this.data = [];
this.func = func;
this.wait = wait;
this.thisArg = thisArg || Object.create(null);
},
push: function(data) {
this.data.push(data);
this.startTimer();
},
startTimer: function() {
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(
this.done.bind(this),
this.wait
);
},
done: function() {
this.func.call(this.thisArg, this.getData());
},
getData: function() {
var latestData = [];
var length = this.data.length;
for (var i = 0; i < length; i++) {
latestData.push(this.data.shift());
}
return latestData;
}
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = waitForIt;
} else {
window.waitForIt = waitForIt;
}
})();