队列运行
我做了一个小库来运行在一组功能具体的队列,
你可以在这里使用它为您的特定情况下,以下内容:
使用
var arr = [1,2,3,4], i = 0;
var fn = function() {
// your business logic is separated from the iteration logic
if(i === arr.length) return;
console.log(arr[i++]);
};
// execute the queue every 1 second
queue([fn], 1000, /* repeat */ true)();
库:
var queue = function(fn,timeout,repeat){
return function(){
var i = 0, f = fn[i], t = setInterval(function(){
f();
if(fn[i+1] !== undefined){
f = fn[++i];
}else{
if(repeat){
i=0;
f = fn[i];
}else{
clearInterval(t);
}
}
}, timeout);
}
}
来源
2015-04-23 12:51:53
amd
看'的setInterval()' –
用递归函数和超时 – adeneo
http://jsfiddle.net/19waccxm/ –