forked from iNavFlight/inav-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintervals.js
143 lines (117 loc) · 3.79 KB
/
intervals.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
'use strict';
var interval = function () {
var privateScope = {},
publicScope = {};
privateScope.intervals = [];
/**
*
* @param {String} name
* @param {Function} code function reference (code to be executed)
* @param {int} interval time interval in milliseconds
* @param {boolean=} first true/false if code should be ran initially before next timer interval hits
* @returns {{name: *, timer: null, code: *, interval: *, fired: number, paused: boolean}}
*/
publicScope.add = function (name, code, interval, first) {
/*
* Kill existing interval with this name if exists
*/
publicScope.remove(name);
var data = {
'name': name,
'timer': null,
'code': code,
'interval': interval,
'fired': 0,
'paused': false
};
if (first == true) {
code(); // execute code
data.fired++; // increment counter
}
data.timer = setInterval(function() {
code();
data.fired++;
}, interval);
privateScope.intervals.push(data);
return data;
};
/**
* Method removes and stop execution of interval callback
* @param {string} name
* @returns {boolean}
*/
publicScope.remove = function (name) {
for (var i = 0; i < privateScope.intervals.length; i++) {
if (privateScope.intervals[i].name == name) {
clearInterval(privateScope.intervals[i].timer); // stop timer
privateScope.intervals.splice(i, 1);
return true;
}
}
return false;
};
/**
*
* @param {string} name
* @returns {boolean}
*/
publicScope.pause = function (name) {
for (var i = 0; i < privateScope.intervals.length; i++) {
if (privateScope.intervals[i].name == name) {
clearInterval(privateScope.intervals[i].timer);
privateScope.intervals[i].paused = true;
return true;
}
}
return false;
};
/**
*
* @param {string} name
* @returns {boolean}
*/
publicScope.resume = function (name) {
for (var i = 0; i < privateScope.intervals.length; i++) {
if (privateScope.intervals[i].name == name && privateScope.intervals[i].paused) {
var obj = privateScope.intervals[i];
obj.timer = setInterval(function() {
obj.code(); // execute code
obj.fired++; // increment counter
}, obj.interval);
obj.paused = false;
return true;
}
}
return false;
};
/**
*
* @param {=} keep_array
* @returns {number}
*/
publicScope.killAll = function (keep_array) {
var timers_killed = 0;
console.log('Killing all intervals except: ' + keep_array);
for (var i = (privateScope.intervals.length - 1); i >= 0; i--) { // reverse iteration
var keep = false;
if (keep_array) { // only run through the array if it exists
keep_array.forEach(function (name) {
if (privateScope.intervals[i].name == name) {
keep = true;
}
});
}
if (!keep) {
clearInterval(privateScope.intervals[i].timer); // stop timer
privateScope.intervals.splice(i, 1); // remove element/object from array
timers_killed++;
}
}
return timers_killed;
};
publicScope.list = function () {
return privateScope.intervals;
};
return publicScope;
}();
module.exports = interval;