我有一个通过 xhr2 获取 PNG 文件的紧密循环。在 FF 和 IE10 中运行良好。在 Chrome 中,当我的文件列表达到大约 5,500 个时,我开始收到 xhr 错误。我喜欢异步接口,因为我将这些请求与本地 indexedDB 存储请求交错。
下面的代码(我使用xhr2lib进行提取,使用PouchDB进行 IndexedDB API)。
我知道是 XHR2 失败了,因为当它工作时,在 Chrome 中,所有 XHR2 调用都在 SaveDB() 调用之前处理。当它失败时,我永远不会收到保存电话。
function getBlobs(fileList) {
console.log("starting to fetch blobs");
$.each(fileList, function (i, val) {
var path = baseURL + val.id + "." + imageType;
$xhr.ajax({
url: path,
dataType: "blob",
success: function (data) { saveBlob(data, val.size, val.id); }
});
});
}
function saveBlob(blob, length, id) {
if (blob.size != length) {
console.error("Blob Length found: " + blob.size + " expected: " + length);
}
putBlob(blob, id);
++fetchCnt;
if (fetchCnt == manifest.files.length) {
setTimeout(fetchComplete, 0);
}
}
function fetchComplete() {
var startTime = vm.get("startTime");
var elapsed = new Date() - startTime;
var fetchTime = ms2Time(elapsed);
vm.set("fetchTime", fetchTime);
}
function putBlob(blob, id) {
var cnt;
var type = blob.type;
DB.putAttachment(id + "/pic", blob, type, function (err, response) {
if (err) {
console.error("Could store blob: error: " + err.error + " reason: " + err.reason + " status: " + err.status);
} else {
console.log("saved: ", response.id + " rev: " + response.rev);
cnt = vm.get("blobCount");
vm.set("blobCount", ++cnt);
if (cnt == manifest.files.length) {
setTimeout(storeComplete, 0);
}
}
});
}