2013-05-27 88 views
1

我有一個緊密的循環,通過xhr2獲取PNG文件。在FF和IE10中正常工作。在Chrome中,當我列出的文件大約有5,500個時,我開始出現xhr錯誤。我喜歡async接口,因爲我將這些請求與本地索引數據庫存儲請求交錯。鉻在太多的xhr請求

以下代碼(我使用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); 
      } 
     } 
    }); 
} 

回答