From 5bb12f7b45d18a84f82c13b269a0227ee1f6df85 Mon Sep 17 00:00:00 2001 From: Tvick22 <142449252+Tvick22@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:15:23 -0800 Subject: [PATCH] fix too many fetches & update every second --- assets/js/revvit/carChat.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/assets/js/revvit/carChat.js b/assets/js/revvit/carChat.js index db639f62..3436b61d 100644 --- a/assets/js/revvit/carChat.js +++ b/assets/js/revvit/carChat.js @@ -48,6 +48,7 @@ async function sendMessage() { } // 3. Return the data + updateMessageDisplay(); return response.json(); // 4. If we had an error anywhere in the try (stuff above), we will log an error. } catch (error) { @@ -72,9 +73,6 @@ async function getMessages() { throw new Error(`Failed to get Messages: ${response.status}`); } - getMessages().then((messages) => { - displayMessages(messages); - }); // 3. Return the data return response.json(); // 4. If we had an error anywhere in the try (stuff above), we will log an error. @@ -97,8 +95,14 @@ function displayMessages(messages) { }); } -getMessages().then((messages) => { - displayMessages(messages); -}); +updateMessageDisplay(); + +function updateMessageDisplay() { + getMessages().then((messages) => { + displayMessages(messages); + }); +} sendBtn.addEventListener("click", sendMessage); + +setInterval(updateMessageDisplay, 1000); // Update the message display every second