javascript 异步队列
(function(win, undefined){ var asycs = win.as = function(ischain){ var me = this; me.ischain = ischain || false; if(!(this instanceof as)){ return new asycs(ischain); } } asycs.prototype.add = function(){//主入口 var arg = Array.prototype.slice.call(arguments), me = this; me.array = arg; me.i = 0; me.reFunc(); } asycs.prototype._listener = function(order){ var me = this; if(--me.length=== 0){ me.callback(); } } asycs.prototype.customShift = function(callback){ var me = this; return function(order){ return callback(function(){ return me.giveMessage(order); }) } } asycs.prototype.reFunc = function(){ var me = this,i = 0 ; var len = me.array.length; me.length = len - 1; me.callback = me.array[len-1]; while(i < len - 1){ var func = me.array[i]; me.array[i] = me.customShift(func); if(!me.ischain){ me.array[i](i); } i++; } if(me.ischain){ me.array[0](0); } return len; } asycs.prototype.giveMessage = function(order){ var me = this; me._listener(order); if(me.ischain){ if(++order < me.array.length - 1){ me.array[order](order); } } return { rec : me.i++, total : "ok" } } })(window, undefined)
根据ischain true a执行完执行b b执行完执行c 全部执行完 执行最后一个函数
false a b c 异步没顺序 但是最后一个函数要等他们执行完再去执行
每执行完一个函数会通知asycs对象 对象接收到后判断是否数组中的最后一个 来执行 回调
切忌浮躁