I am attempting to execute a chunk of code that is resource intensive to the point it locks the browser while executing. As a workaround, I am trying to use mbostock's queue.js library in conjunction with setTimeout to rate-limit the execution such that the lockup is not noticed by the user. The calls must be made in a specific order but need to have a breathing period in between (currently I'm using 25ms).
I am having trouble figuring out how to link things together.
Specifically, there seems to be an issue with the availability of the 'param' variable inside the setTimeout function scope. When this code executes, param ends up being the same instance for every iteration, not a different instance, as expected.
var q = queue(1);
var waitParamInits = [];
var keys = Object.keys(attr.customParams);
for (var i=0; i < keys.length; i++) {
var param = attr.customParams[keys[i]];
var wait = function(callback) {
setTimeout(function() {
if (!param.initializeParm()) {
handleError('Error initializing parameter: ' + param, false);
}
}, 25, param);
};
waitParamInits.push(wait);
}
waitParamInits.forEach(function(t) {q.defer(t); });
q.awaitAll(function(error, results) {
console.log('finished with wait params');
});