cryptalk/public/js/cryptalk_modules/queue.js

44 lines
962 B
JavaScript
Raw Normal View History

2014-09-21 13:53:57 -04:00
define(function (){
var exports = {},
queue = [],
now = function () {
return performance.now() || Date.now();
};
exports.add_function_delayed = function(delay, callback, data) {
queue.push({
func: callback,
pushed: now(),
delay: delay,
data: data
});
2014-09-20 09:26:43 -04:00
}
2014-09-21 13:53:57 -04:00
exports.get = function () {
2014-09-20 09:26:43 -04:00
return queue;
}
2014-09-21 13:53:57 -04:00
exports.run = function () {
var i = 0,
current,
lrt_inner;
2014-09-20 09:26:43 -04:00
2014-09-21 13:53:57 -04:00
while (current = queue[i++]) {
if (now() - current.pushed > current.delay) {
2014-09-20 09:26:43 -04:00
current.func();
2014-09-21 13:53:57 -04:00
queue.splice(i - 1, 1);
2014-09-20 09:26:43 -04:00
}
}
2014-09-21 13:53:57 -04:00
if (queue.length) {
// Waste a ms to prevent callstack overflow
lrt_inner = now();
while (now() - lrt_inner < 1) { void 0; };
2014-09-20 09:26:43 -04:00
2014-09-21 13:53:57 -04:00
exports.run();
}
2014-09-20 09:26:43 -04:00
}
return exports;
});