1 function multistep(steps, args, callback){ 2 var tasks = steps.concat(); 3 4 setTimeout(function(){ 5 var task = tasks.shift(); 6 7 task.apply(null, args || []); // call / aplly 第一个参数是 null或者undefined的时候指向window 或者 Global 8 9 if(tasks.length > 0){ // 检测还有没有其他步骤要处理 10 setTimeout(arguments.callee, 25); 11 }else{ 12 callback(); 13 } 14 }, 25); 15 } 16 17 function saveDocument(id,nd){ // 注意传参 18 var tasks = [openDocument, writeText, closeDocument, updateUI]; 19 20 multistep(tasks, [id, nd], function(){ 21 console.log("Save completed!"); 22 }); 23 } 24 25 saveDocument({color : "red"},{name : "NICK"}); // 注意传参 26 function openDocument(n,m){console.log(n,m)} 27 function writeText(n){console.log(n)} 28 function closeDocument(n){console.log(n)} 29 function updateUI(n){console.log(n)}