隊列運行
我做了一個小庫來運行在一組功能具體的隊列,
你可以在這裏使用它爲您的特定情況下,以下內容:
使用
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/ –