let queue = JSON.parse(localStorage.getItem("offlineQueue")) || [];
if (Array.isArray(queue)) {
queue.forEach(function (entry) {
- updateRemote(entry.data);
+ updateRemote(entry.data, new Date(entry.time));
});
localStorage.removeItem("offlineQueue");
}
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ā¦";
}
}
-function hiddenUpdateRemote(json) {
+function hiddenUpdateRemote(json, time = new Date()) {
var msg = JSON.parse(json);
switch(msg.type) {
case "keys":
var flavor = getFlavor(msg.key);
if (flavor !== "") {
- addCoffee(flavor);
+ addCoffee(flavor, time);
}
break;
case "ajax_failure":
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);
}
}
loadRemote();
function ajax(method, route, data, id) {
- var now = Date.now();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
} else {
updateRemote = undefined;
updateUI();
+ loadRemote(); // Try to contact the server periodically
if (localStorage) {
var ajax_failure = JSON.stringify({
}
}
-function addCoffee(flavor) {
+function addCoffee(flavor, time = new Date()) {
var data = JSON.stringify({
- time: new Date().toISOString(),
+ time: time.toISOString(),
flavor: flavor,
uid: id_user
});