File size: 1,065 Bytes
3a4c55d
 
12aae0b
3a4c55d
 
 
 
 
12aae0b
3a4c55d
 
 
12aae0b
3a4c55d
 
12aae0b
3a4c55d
 
 
 
12aae0b
3a4c55d
 
 
12aae0b
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
38
window.onload = () => {
  const s = new Speedtest();

  document.getElementById("startBtn").addEventListener("click", () => {
    s.onupdate = data => {
      let dl = parseFloat(data.dlStatus || 0).toFixed(2);
      let ul = parseFloat(data.ulStatus || 0).toFixed(2);
      let ping = parseFloat(data.ping || 0).toFixed(2);

      document.getElementById("dl").textContent = dl;
      document.getElementById("ul").textContent = ul;
      document.getElementById("ping").textContent = ping;

      animateGauge(Number(dl));
    };

    s.onend = () => {
      console.log("Test completed.");
      saveToLocal();
    };

    s.start();
  });
};

function saveToLocal() {
  const result = {
    dl: document.getElementById("dl").textContent,
    ul: document.getElementById("ul").textContent,
    ping: document.getElementById("ping").textContent,
    date: new Date().toLocaleString()
  };

  let history = JSON.parse(localStorage.getItem("speedTestHistory") || "[]");
  history.push(result);
  localStorage.setItem("speedTestHistory", JSON.stringify(history));
}