这应该做的工作:
// callAll : calls methodName method of all array items.
// uses the provided arguments for the call and adds the callback
// methodName is async and must accept a callback as last argument
// lastCallBack will get called once after all methods ended.
//
function callAll(anArray, methodName, lastCallBack) {
// create closure to keep count of calls
var callCount = anArrray.length;
// build function that calls lastCallBack on last call
var callIfLast = function() { callCount--; if (!callCount) lastCallBack(); };
// build function arguments
var args = arguments.slice(3).push(callIfLast);
// call all functions
anArray.forEach(function(item) { item[methodName].apply(item, args) });
}
callAll(myArray, 'meth', myCallback, 1, 2, 3);
// ...
// --> will call myArray[i].meth(1, 2, 3, callIfLast) for all i.
// and call myCallBack when all calls finished.
参数传递给你的回调,表明这是否是最后一个 –