From 2309172bfc18a57a42e50ced623db0b29406047a Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Thu, 7 May 2020 16:11:05 +0530 Subject: [PATCH] Add retry for assets --- shell.html | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/shell.html b/shell.html index 42709f6..70af878 100644 --- a/shell.html +++ b/shell.html @@ -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;