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