Looping over two different arrays, both for-loops nested within each other asynchronously (as chunks and setTimeout
sourced from here), and trying to use the progress bar example from W3Schools here (the Label one).
The sourced (slightly modified for a 'done' callback) asynchronous function:
function loopAsync(array, fn, done, chunk, context) {
context = context || window;
chunk = chunk || 10;
var index = 0;
function doChunk() {
var cnt = chunk;
while (cnt-- && index < array.length) {
// callback called with args (value, index, array)
fn.call(context, array[index], index, array);
++index;
//console.log("index is " + index);
progressBar(index);
}
if (index < array.length) {
// set Timeout for async iteration
setTimeout(doChunk, 1);
} else {
done && done();
}
}
doChunk();
}
Regardless of being asnychronous, these are the same problems even with a normal for-loop:
The W3School example is using
setInterval
, which is inaccurate since the for-loops may already finish processing beforesetInterval
is.There are two nested for-loops, so instead of tracking the progress of (for example)
i
infor (var i=0...)
, it needs to track the first loop * the second loop for accuracy (as to not appear stopped - especially because the second loop will likely have a larger array length than the first).
For example:
Asynchronously using the linked example above:
loopAsync(arr1, function (item1) {
loopAsync(arr2, function (item2) {
//Comparing or processing item1 with item2
});
}, doNext);
Or, basically the same without asynchronous looping:
for (var item1 = 0; item1 < arr1.length; ++item1) {
for (var item2 = 0; item2 < arr2.length; ++item2) {
//Doing things... need to track progress of both?
}
}
- Needs to be generic, able to be used in any nested (or non-nested) for-loop operation.
How should these problems be addressed, preferably without jquery?
i think thats just need basic increment. you can use something like this:
another example with random execution time (use promise to doing async process)