mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-13 11:29:53 +03:00
timing methods extracted to separate classes
This commit is contained in:
parent
fc11eca5c0
commit
d1fcb81017
26 changed files with 262 additions and 219 deletions
66
js/timeouts.js
Normal file
66
js/timeouts.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
'use strict';
|
||||
|
||||
var helper = helper || {};
|
||||
|
||||
helper.timeout = (function () {
|
||||
|
||||
var privateScope = {},
|
||||
publicScope = {};
|
||||
|
||||
privateScope.timeouts = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {function } code
|
||||
* @param {number} timeout
|
||||
* @returns {{name: *, timer: null, timeout: *}}
|
||||
*/
|
||||
publicScope.add = function (name, code, timeout) {
|
||||
var data = {'name': name, 'timer': null, 'timeout': timeout};
|
||||
|
||||
// start timer with "cleaning" callback
|
||||
data.timer = setTimeout(function() {
|
||||
code(); // execute code
|
||||
|
||||
// remove object from array
|
||||
var index = privateScope.timeouts.indexOf(data);
|
||||
if (index > -1) privateScope.timeouts.splice(index, 1);
|
||||
}, timeout);
|
||||
|
||||
privateScope.timeouts.push(data); // push to primary timeout array
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
publicScope.remove = function (name) {
|
||||
for (var i = 0; i < privateScope.timeouts.length; i++) {
|
||||
if (privateScope.timeouts[i].name == name) {
|
||||
clearTimeout(privateScope.timeouts[i].timer); // stop timer
|
||||
privateScope.timeouts.splice(i, 1); // remove element/object from array
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {number} number of killed timeouts
|
||||
*/
|
||||
publicScope.killAll = function () {
|
||||
var timers_killed = 0;
|
||||
|
||||
for (var i = 0; i < privateScope.timeouts.length; i++) {
|
||||
clearTimeout(privateScope.timeouts[i].timer); // stop timer
|
||||
timers_killed++;
|
||||
}
|
||||
|
||||
privateScope.timeouts = []; // drop objects
|
||||
|
||||
return timers_killed;
|
||||
};
|
||||
|
||||
return publicScope;
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue