Skip to content

Instantly share code, notes, and snippets.

@lichenbuliren
Created September 13, 2016 09:16
Show Gist options
  • Save lichenbuliren/d5e7c3b5748bab98dcc52cfa89202889 to your computer and use it in GitHub Desktop.
Save lichenbuliren/d5e7c3b5748bab98dcc52cfa89202889 to your computer and use it in GitHub Desktop.
/**
* 分时函数例子
* 以创建 WebQQ 列表为例
* @param {[type]} data 函数执行需要用到的数据
* @param {Function} fn 真正需要分时执行的函数
* @param {[type]} count 每次创建一批节点的数量
* @param {[type]} interval 函数执行间隔
* @return {[type]} [description]
*/
var timeChunk = function(data, fn, count, interval) {
var t;
var len = data.length;
var start = function() {
for (var i = 0; i < Math.min(count || 1, data.length); i++) {
var obj = data.shift();
fn(obj);
}
}
return function() {
t = setInterval(function() {
if (data.length === 0) {
return clearInterval(t);
}
start();
}, interval);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment