You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
inav-configurator/js/timeouts.js

66 lines
1.7 KiB
JavaScript

'use strict';
var 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;
})();
module.exports = timeout;