From 08287a9758184f5cf456456c5d84e44631f4e065 Mon Sep 17 00:00:00 2001 From: arianagiroux Date: Thu, 4 Aug 2022 17:36:01 -0600 Subject: [PATCH] Simplified javascript function for updating UI - breaks uiUpdate() into two functions - uiUpdateOnce(), which contains the bulk of the UI updating code - continuousUiUpdate(), which runs the aforementioned function once every second. - updates updateStreamTitle() to conform to this new change - removes call to Main(), replacing it with a call to the new continuousUiUpdate() --- static/script.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/static/script.js b/static/script.js index d9e4f3f..ec6c773 100644 --- a/static/script.js +++ b/static/script.js @@ -4,29 +4,35 @@ async function getEndpoint(url = '') { return data; } -async function uiUpdate() { +async function uiUpdateOnce() { + var data = await getEndpoint('http://127.0.0.1:5000/api/serverstatus'); + document.getElementById("streamTitle").innerHTML = data.streamTitle; + document.getElementById("currentViewers").innerHTML = data.viewerCount; + document.getElementById("sessionMaxViewerCount").innerHTML = data.sessionMaxViewerCount; + document.getElementById("overallMaxViewerCount").innerHTML = data.overallMaxViewerCount; + document.getElementById("tags").innerHTML = data.tags; +} + +async function continuousUiUpdate() { window.setInterval(async () => { - data = await getEndpoint('http://127.0.0.1:5000/api/serverstatus'); - document.getElementById("streamTitle").innerHTML = data.streamTitle - document.getElementById("currentViewers").innerHTML = data.viewerCount - document.getElementById("sessionMaxViewerCount").innerHTML = data.sessionMaxViewerCount - document.getElementById("overallMaxViewerCount").innerHTML = data.overallMaxViewerCount - document.getElementById("tags").innerHTML = data.tags - }, 1000) + uiUpdateOnce(); + }, 1000); } async function updateStreamTitle(value) { + // this function simply passes data along to the python script, and allows + // python to handle data processing response = await fetch('http://127.0.0.1:5000/api/update/streamtitle', { method:'POST', body:JSON.stringify(value) } ); - await uiUpdate() + await uiUpdateOnce(); } async function Main() { await uiUpdate() } -Main() +continuousUiUpdate()