From f0b87de63dedaa39b28359d5e5dfb484bc7921e2 Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Wed, 18 Dec 2024 12:30:41 +0100 Subject: [PATCH] switched to older version --- src/Python/flask/web/static/script.js | 55 ++++++++++----------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/src/Python/flask/web/static/script.js b/src/Python/flask/web/static/script.js index cdd56e4..109916d 100644 --- a/src/Python/flask/web/static/script.js +++ b/src/Python/flask/web/static/script.js @@ -1,36 +1,32 @@ -document.addEventListener("DOMContentLoaded", function() { - document.querySelectorAll(".btn").forEach(button => { - button.addEventListener("click", function(event) { - event.preventDefault(); // prevents page refresh +// Selecteer alle knoppen en voeg een event listener toe aan elke knop +document.querySelectorAll(".btn").forEach(button => { + button.addEventListener("click", function(event) { + event.preventDefault(); // voorkomt pagina-verversing - // Get the value of the button - const direction = event.target.value; + // Haal de waarde van de knop op + const direction = event.target.value; - fetch("/move", { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ direction: direction }) - }) - .then(response => response.json()) - .then(data => {script - console.log("Success:", data); - }) - .catch(error => { - console.error("Error:", error); - }); + fetch("/move", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ direction: direction }) + }) + .then(response => response.json()) + .then(data => { + console.log("Success:", data); + }) + .catch(error => { + console.error("Error:", error); }); }); // Fetch data from the server async function fetchData() { - try{ const response = await fetch("/data"); const data = await response.json(); return data; - } catch (error) { - console.error("Error:", error); } // Parse the data and show it on the website @@ -38,7 +34,7 @@ document.addEventListener("DOMContentLoaded", function() { const data = await fetchData(); const sensorDataContainer = document.getElementById("sensor-data"); sensorDataContainer.innerHTML = ""; // Clear previous data - // For each object in JSON array, create a new paragraph element and append it to the sensorDataContainer + //for each object in json array create a new paragraph element and append it to the sensorDataContainer for (const [key, value] of Object.entries(data)) { const dataElement = document.createElement("p"); dataElement.textContent = `${key}: ${value}`; @@ -46,15 +42,6 @@ document.addEventListener("DOMContentLoaded", function() { } } - // Update the image - function updateImage() { - var img = document.getElementById("robot-image"); - img.src = "/image?" + new Date().getTime(); // Add timestamp to avoid caching - } - // Fetch and display sensor data every 5 seconds - setInterval(parseData, 1000); - - // Update the image every 5 seconds - setInterval(updateImage, 200); + setInterval(parseData, 5000); }); \ No newline at end of file