Add retry for assets

This commit is contained in:
Varun Patil 2020-05-07 16:11:05 +05:30
parent 00ccbf5db9
commit 2309172bfc
1 changed files with 13 additions and 1 deletions

View File

@ -230,9 +230,15 @@
const xhr = new XMLHttpRequest();
const pdiv = document.getElementById("progress");
let showTimer = 0;
let abortTimer = 0;
const retry = () => {
xhr.abort();
getLazyAsset(url, filename, callback);
}
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status >= 200 && xhr.status < 400) {
pdiv.innerHTML = `${filename} - done`;
hideTimer = setTimeout(() => {
pdiv.style.opacity = '0';
@ -241,12 +247,16 @@
callback();
clearTimeout(showTimer);
clearTimeout(abortTimer);
}
}
xhr.onprogress = function (event) {
const loaded = Math.round(event.loaded / 1024);
const total = Math.round(event.total / 1024);
pdiv.innerHTML = `${filename} - ${loaded}KB / ${total}KB`;
clearTimeout(abortTimer);
abortTimer = setTimeout(retry, 3000);
};
xhr.open('GET', url);
xhr.send();
@ -257,6 +267,8 @@
pdiv.style.opacity = '0.5';
}, 100);
abortTimer = setTimeout(retry, 3000);
if (hideTimer) {
clearTimeout(hideTimer);
hideTimer = 0;