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     });

 

相关文章:

  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2021-11-18
  • 2021-08-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-09-21
  • 2022-12-23
  • 2021-09-22
  • 2021-06-20
相关资源
相似解决方案