summaryrefslogtreecommitdiff
path: root/priv/static/room/displayState.js
blob: a2da229dee277635e7538a42c2ac76b8406fa62d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"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(`Fetching room state failed with status code ${response.status}`);
          }
          return response.json();
        })
        .then((json) => {
          const viewersConnected = json.viewersConnected;
          stateElement.dataset.text = `total viewers: ${viewersConnected}`;
        })
        .catch((error) => {
          console.error(error);
          stateElement.dataset.text = STATE_ELEMENT_INITIAL_TEXT;
        });
    }
  }, INTERVAL_MILLISECONDS);
})();