]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
When the server goes offline, try to teload the page automatically
authorMichal Sojka <michal.sojka@cvut.cz>
Tue, 14 Aug 2018 23:04:27 +0000 (01:04 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Wed, 15 Aug 2018 05:34:37 +0000 (07:34 +0200)
templates/main.js

index 561bfa769fb636617e65fd9de4427a852855dbd9..63aa40dcad4dd513d2afa3f4bbcd5572360e174a 100644 (file)
@@ -6,6 +6,7 @@ var updateRemote = undefined;   // defined iff remote server accessible
 var loggedIn = false;           // true after swiping the cards
 var timeToLogout = undefined;   // defined during logout countdown
 var logoutTimer;
+var reloadTimer = undefined;
 
 console.log("hello from flask");
 //sendJSON("{\"type\":\"empty\"}");
@@ -47,6 +48,7 @@ function updateUI()
     if (updateRemote === undefined) {
         update("remote", "<center>Server offline...</center>");
         document.getElementById("local").style.display = "block";
+        loadRemote();
     } else {
         document.getElementById("local").style.display = "none";
 
@@ -96,11 +98,16 @@ function hiddenUpdateRemote(json) {
 function loadRemote(string) {
     var xhr = new XMLHttpRequest();
     xhr.onreadystatechange = function() {
-        if (this.readyState == 4 && this.status == 200) {
-            update("remote", this.responseText);
-            updateRemote = hiddenUpdateRemote;
-            loadLocalStorage();
-            updateUI();
+        if (this.readyState == 4) {
+            if (this.status == 200) {
+                update("remote", this.responseText);
+                updateRemote = hiddenUpdateRemote;
+                loadLocalStorage();
+                updateUI();
+                clearTimeout(reloadTimer);
+            } else {
+                reloadTimer = setTimeout(loadRemote, 1000);
+            }
         }
     };
     xhr.open("GET", flask, true);