用定时器处理数组 setTimeout()

 1 // 用定时器处理数组
 2     var items = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
 3 
 4     function processArray(items, process, callback){
 5         var todo = items.concat();    // 克隆原数组
 6 
 7         setTimeout(function(){
 8             process(todo.shift());    // 取得数组的下个元素并进行处理
 9 
10             // 如果还有需要处理的元素,创建另一个定时器
11             if(todo.length > 0){
12                 setTimeout(arguments.callee, 25);
13             }else{
14                 callback(items);
15             }
16         }, 25);
17     }
18 
19     function outputValue(value){
20         console.log(value);
21     }
22 
23     processArray(items, outputValue, function(){
24         console.log("You have done it!");
25     });
疯癫不成狂,有酒勿可尝;世间良辰美,终成水墨白。
原文地址:https://www.cnblogs.com/chuyu/p/3345672.html