๐Ÿ“ฆ Chocorean / chocorean.github.io

๐Ÿ“„ services.js ยท 28 lines
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
27
28function updateStatus(id, status) {
  // find the td node with given id in plain js
  var td = document.getElementById(id);
  // set the status in the inner html
  td.innerHTML = status ? "โœ…" : "โŒ";

}

function serviceStatus(name, link, expected) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', link, true);
  xhr.timeout = 5000;
  xhr.onreadystatechange = function () {
    // when xhr is done
    if (xhr.readyState == 4) {
      updateStatus(name, xhr.status == expected);
    }
  };
  xhr.ontimeout = function () {
    console.error('The request for ' + link + ' timed out.');
    updateStatus(name, false);
  };
  xhr.onerror = function () {
    console.error('The request for ' + link + ' failed.');
    updateStatus(name, false);
  }
  xhr.send();
}