From 5d958387697357bb1fc06088a50eee71fb9f2b72 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 2 Jan 2023 22:53:52 +0100 Subject: [PATCH] Disable web when losing connection --- data/www/script.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/data/www/script.js b/data/www/script.js index 50a1f2d..8799441 100644 --- a/data/www/script.js +++ b/data/www/script.js @@ -2,7 +2,8 @@ const GLOBAL_TIMEOUT = 7000; function play() { console.log("Play..."); - fetch("/play", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) }); + fetch("/play", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) }) + .catch(handleError); } function volume(modifier) { @@ -10,14 +11,16 @@ function volume(modifier) { body.set("modifier", modifier); fetch(`/change-volume`, { method: "POST", body: body, signal: AbortSignal.timeout(GLOBAL_TIMEOUT) }) .then(response => response.json()) - .then(handleStatus); + .then(handleStatus) + .catch(handleError); } function loadStatus() { console.log("Status..."); fetch("/status", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) }) .then(response => response.json()) - .then(handleStatus); + .then(handleStatus) + .catch(handleError); } function selectFile(name) { @@ -26,10 +29,12 @@ function selectFile(name) { body.set("fileName", name); fetch("/select-file", { method: "POST", body: body, signal: AbortSignal.timeout(GLOBAL_TIMEOUT) }) .then(response => response.json()) - .then(handleStatus); + .then(handleStatus) + .catch(handleError); } function handleStatus(data) { + document.body.classList.remove("w3-disabled"); console.log("data", data); let dom = ""; data.files.forEach(element => { @@ -46,7 +51,12 @@ function handleStatus(data) { document.getElementById("volume-decrease").disabled = !data.volume.canDecrease; } +function handleError() { + console.log("Lost connection :'("); + document.body.classList.add("w3-disabled"); +} + (() => { loadStatus(); - setInterval(loadStatus, 10000); + setInterval(loadStatus, GLOBAL_TIMEOUT); })();