用定时器处理数组 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值