]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Do not allow multiple reconnect timers running simultaneously
authorMichal <michal.sojka@cvut.cz>
Wed, 15 Aug 2018 10:14:06 +0000 (12:14 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Thu, 16 Aug 2018 21:27:37 +0000 (23:27 +0200)
It is unnecessary waste of resources.

templates/main.js

index 7f63cc52f8e41fc2adb54302eb81055ed9e306b6..02c956c33a0dd30b5badb90efdcb2c82f7454bf6 100644 (file)
@@ -40,10 +40,8 @@ function updateUI()
         document.getElementById("local").style.display = !offline ? "none" : "block";
         document.getElementById("remote").style.display = offline ? "none" : "block";
 
-        if (offline) {
-            loadRemote();       // Try to contact the server periodically
+        if (offline)
             return;
-        }
 
         if (id_user !== undefined) {
             document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā€¦";
@@ -92,6 +90,10 @@ function loadRemote(string) {
                 updateUI();
                 clearTimeout(reloadTimer);
             } else {
+                // Cancel current timer for the case when loadRemote()
+                // was called multiple times (e.g. multiple ajax()
+                // calls failed simultaneously).
+                clearTimeout(reloadTimer);
                 reloadTimer = setTimeout(loadRemote, 1000);
             }
         }
@@ -112,6 +114,7 @@ function ajax(method, route, data, id) {
             } else {
                 updateRemote = undefined;
                 updateUI();
+                loadRemote(); // Try to contact the server periodically
 
                 if (localStorage) {
                     var ajax_failure = JSON.stringify({