-
-
Save leeseean/414f7991eaaa97afe09d0901368493eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 分时函数例子 | |
* 以创建 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