我正在嘗試在ajax請求響應文本中查找圖像並獲取在另一個ajax請求中使用的src url。在xhr onprogress中查找兩個異步ajax請求的結果
我需要知道加載每個圖像的進度,並在進度條中顯示此進度的結果。 但是,正如你可以看到如果image1加載10%; 和image2 20%。
我的結果從20%降到10%。 我的進度條向後移動,然後向前移動。
任何幫助? 或者這是一個很好的解決方案?
parseResult: function(result, navNumber) {
var self = this;
var imagesMatter = $(result).find('img');
var imagesLength = imagesMatter.length;
// if there is any image in ajax request then fetch imgs using ajax for show progress bar
if (imagesLength >= 1) {
var i = 0,
complete = 0,
temp = 1;
navigation.progress.fadeIn(50);
imagesMatter.each(function() {
var $this = $(this);
var URL = $this.attr('src');
$.ajax({
url: URL,
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = parseFloat(evt.loaded/evt.total).toFixed(1);
temp = complete + (percentComplete/imagesLength);
console.log(parseInt(temp * 100) + '%');
navigation.progress.html(parseInt(temp * 100) + '%');
}
}, false);
return xhr;
},
complete: function() {
complete = temp;
i++;
if (i == imagesLength) {
self.closeLoading(navNumber);
}
}
});
});
} else {
return;
}
}
你不應該設置'complete = temp; 「內部進度回調,而不僅僅是內在完整的回調? –
@amif:如果我理解正確,你想要顯示兩個圖像加載單進度條,對嗎? –
jsfiddle請 –