-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeticker.js
More file actions
115 lines (113 loc) · 3.86 KB
/
Copy pathtimeticker.js
File metadata and controls
115 lines (113 loc) · 3.86 KB
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
var ticker = function(name, starttime, endtime, mode, intervalorbrust) {
function computeTimeInterval(time) {
var computedinterval = time - new Date();
if (computedinterval >= 0)
return computedinterval;
else return 0;
}
//checking is their end time is before start time
function checkStartAndEnd(start, end) {
if (end)
if (start < end) {
return true;
} else return false;
else return true;
}
function checkArgument() {
if (mode == 'continuous' || mode == 'none' || mode == 'burst') {
return true;
} else {
return false;
}
}
//utility function to generate a irregular interval
function randomTimeInterval(end, count) {
return (Math.random() * ((end - count) - new Date()) + 1)
}
return {
name: function() {
return this.conf.name;
},
//self object returns the dependent configuration
conf: {
name: name || "ticker",
end: endtime || undefined,
start: starttime || undefined,
intervalorburst: intervalorbrust || 100,
timer: undefined,
mode: mode || 'none',
running: false,
scheduled: false,
starttimer: undefined,
endtimer: undefined,
currentcount: 0
},
//function to call the ticker instantiate
start: function(cb) {
var self = this;
if (self.conf.start) {
if (self.conf.scheduled) {
self.conf.running = true;
if (self.conf.mode == 'continuous' || self.conf.mode == 'none') {
self.conf.timer = setInterval(cb, self.conf.intervalorburst);
} else if (self.conf.mode == 'burst') {
var recursivecb = function() {
cb();
if (self.conf.currentcount < self.conf.intervalorburst - 1) {
self.conf.currentcount++;
self.conf.timer = setTimeout(recursivecb, randomTimeInterval(self.conf.end, self.conf.currentcount))
}
}
self.conf.timer = setTimeout(recursivecb, randomTimeInterval(self.conf.end, self.conf.currentcount))
}
} else self.scheduler(cb);
} else {
self.conf.running = true;
if (self.conf.mode == 'continuous' || self.conf.mode == 'none') {
self.conf.timer = setInterval(cb, self.conf.intervalorburst);
} else if (self.conf.mode == 'burst') {
var recursivecb = function() {
cb();
if (self.conf.currentcount < self.conf.intervalorburst) {
self.conf.currentcount++;
self.conf.timer = setTimeout(recursivecb, randomTimeInterval(self.conf.end, self.conf.currentcount))
}
}
self.conf.timer = setTimeout(recursivecb, randomTimeInterval(self.conf.end, self.conf.currentcount))
}
}
},
//scheduler to schedule when to call start and when to call stop
scheduler: function(startcb, stopcb) {
var self = this;
var starttimeout = computeTimeInterval(self.conf.start);
if (self.conf.end)
var endtimeout = computeTimeInterval(self.conf.end);
if (checkStartAndEnd(starttimeout, endtimeout)) {
self.conf.scheduled = true;
self.conf.starttimer = setTimeout(function() { self.start(startcb) }, starttimeout);
if (endtimeout) self.conf.endtimer = setTimeout(function() {
self.stop(stopcb);
}, endtimeout);
} else {
throw "start and time not set properly";
}
},
//stop the the ticker
stop: function(cb) {
var self = this;
clearInterval(self.conf.timer);
self.conf.timer = undefined;
self.conf.running = false;
if (cb) cb();
},
cancelschedular: function() {
clearTimeout(self.conf.starttimer);
clearTimeout(self.conf.endtimer);
self.conf.starttimer = undefined;
self.conf.endtimer = undefined;
self.conf.scheduled = false;
}
}
}
module.exports = ticker;