[HTML]代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title> </title> </head> <body> <script> //并行执行 var parall=function(arr,call){ if(!(arr&&arr.length))return; var endfn=(function(len,call){ var results=[]; return function(data){ results.push(data); if(results.length==len)call(results); } })(arr.length,call); for(var i=0,item;item=arr[i++];){ item(endfn); } } //序列执行 var sear=function(arr,call){ if(!(arr&&arr.length))return; var i=0; !function endfn(data){ if(!arr[i]){ call(data); return; } if(data)arr[i++](data,endfn); else arr[i++](endfn); }(); } //TEST parall([function(call){ setTimeout(function(){ console.log('one') call('one'); },1000); },function(call){ setTimeout(function(){ console.log('two') call('two'); },1000); }],function(r){ console.log(r); }) sear([function(call){ setTimeout(function(){ console.log('one') call('one'); },1000); },function(para,call){ setTimeout(function(){ console.log(para+'two') call(para+'two'); },1000); }],function(r){ console.log(r); }) </script> </body> </html>
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/8753.html