First of all, you are not pushing functions in the array at the moment, instead you are executing func. To get the push, your function should look like this:
// Function for array objects - alert passed parameter function func(num){ return function(){ alert(num); } }
Now, if your functions are synchronous, you can just iterate over the array
for(var i in arr){ arr[i](); } console.log('done');
If we are dealing with asynchronous functions, then they must have a callback:
// Function for array objects - alert passed parameter function func(num){ return function(callback){ alert(num); callback(); } }
And then you can use the counter for parallel operation.
var count = arr.length; for(var i in arr){ arr[i](function(){ if(--count === 0){ console.log('Done'); } }); }
Or sequentially:
function run(){ var fn = arr.shift(); if(!fn){ console.log('Done'); } else { fn(run); } } run();
source share