diff options
Diffstat (limited to 'priv/static/room/displayState.js')
-rw-r--r-- | priv/static/room/displayState.js | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/priv/static/room/displayState.js b/priv/static/room/displayState.js new file mode 100644 index 0000000..3a5e923 --- /dev/null +++ b/priv/static/room/displayState.js @@ -0,0 +1,25 @@ +"use strict"; + +(() => { + const INTERVAL_MILLISECONDS = 3000; + + setInterval(() => { + const stateElement = document.getElementsByClassName("state-element").item(0); + if (stateElement !== null) { + fetch(STATE_URL) + .then((response) => { + if (!response.ok) { + throw new Error(`Error: fetching state returned ${response.status}`); + } + return response.json(); + }) + .then((json) => { + const viewersConnected = json.viewersConnected; + stateElement.dataset.text = `total viewers: ${viewersConnected}`; + }) + .catch((_) => { + stateElement.dataset.text = STATE_ELEMENT_INITIAL_TEXT; + }); + } + }, INTERVAL_MILLISECONDS); +})(); |