aboutsummaryrefslogtreecommitdiff
path: root/priv/static/room/displayState.js
blob: 3a5e923fda53fb120c7c18d374b18c58c4ef111b (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
"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);
})();