代码如下:
var timeChunk = function(data, fn, count, time) {
var that = this,
obj,
index = 0,
timer;
var start = function() {
index += 1;
for (var i = 0; i < Math.min(count || 1, (data.length + 1)); i++) {
console.log('start次数:' + index + ',i值:' + i + ', data长度:' +
(data.length + 1))
console.log(Math.min(count || 1, (data.length + 1)))
obj = data.shift();
fn(obj);
}
}
return function() {
timer = setInterval(function() {
if (data.length === 0) return clearInterval(timer);
start();
}, count)
}
}
var arr = [
'nice to meet you',
'hello',
'xixi',
'nice to meet you',
'hello',
'xixi'
];
var getMsgToDo = function(obj) {
var div = document.createElement('div');
div.innerHTML = obj;
document.body.appendChild(div);
}
var renderList = timeChunk(arr, getMsgToDo, 3, 200);
renderList()
最终start执行了3次 我传入的是3 共有6条数据 应该执行2次才对
经过排查 start在执行第二次的时候 i=2时退出了循环 但我不知道为什么会这样
犯罪嫌疑人X
相关分类