-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththymer.js
41 lines (39 loc) · 875 Bytes
/
thymer.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
var Q = require('q');
var thymer = (function() {
return {
deffered:Q.defer(),
timeout:null,
interval:null,
limit:null,
wait: function(n){
this.timeout = n;
return this;
},
every: function(n){
this.interval = n;
return this;
},
for: function(n){
this.limit = n;
return this.interval ? this : console.error('every for needs an every');
},
do: function(f){
var self = this;
var goal = function(){
var sub = function(){
f();
self.limit ? self.limit-- : undefined;
self.limit === 0 ? self.stop() : undefined;
}
self.interval ? self.interval = setInterval(sub, self.interval) : sub();
}
this.timeout ? setTimeout(goal, this.timeout) : goal();
return this.deffered.promise;
},
stop: function(){
clearInterval(this.interval);
this.deffered.resolve();
}
};
})()
module.exports = thymer;