如何在转换数组的Underscore.js _.each
循环中设置延迟?这是我目前的代码:
_.each(boundaries.features, function(feature, i) {
feature = _this.getBoundary(feature, values, activeNames);
});
现在我想在每千次循环中添加一个暂停,这样脚本就不会锁定IE8。
首次尝试:
_.each(boundaries.features, function(feature, i) {
feature = _this.getBoundary(feature, values, activeNames);
// Stop IE locking up.
if (i % 1000 === 0) {
function assign() {
feature = _this.getBoundary(feature, values, activeNames);
}
window.setTimeout(assign, 20);
}
});
很确定这不会起作用。
更新:第二次尝试,仅为IE8添加条件。这看起来有点模糊吗?
var newData = [];
function iterateOverItems(items, startItem) {
for (i = startItem; i < items.length; i++) {
newData.push(transformItem(items[i]));
if ((i % 1000 === 0) && (!document.addEventListener)) {
window.setTimeout(iterateOverItems, 100);
break;
}
}
iterateOverItems(items, 0);
呃,这不会起作用,因为我无法将所需的参数传递给递归函数。有什么想法吗?