Skip to content

Commit

Permalink
added update timer
Browse files Browse the repository at this point in the history
  • Loading branch information
r-downing committed Nov 7, 2017
1 parent c8f9895 commit 7183b65
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions examples/basic_rest_api/data/index.htm
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<h3>Current Data <span id="status"></span></h3>
x: <span id="x"></span><br/>
y: <span id="y"></span>
<h3>Current Data</h3>
x:
<span id="x"></span>
<br/> y:
<span id="y"></span>

<h6>Last updated
<span id="ut"></span> seconds ago.
<span id="status"></span>
</h6>

<h3>Update Data</h3>
<form id="xform" onsubmit="return transmit(this)">
Expand All @@ -18,26 +25,28 @@ <h3>Update Data</h3>

<script>
function g(i) { return document.getElementById(i) };
var xhttp;
var xhttp, updateTime;

function transmit(f) {
if (!xhttp) { //prevent simultaneous requests
g("status").innerHTML = "updating...";
xhttp = new XMLHttpRequest();
xhttp.open("POST", "/api");
xhttp.send(f?(new FormData(f)):"");
xhttp.send(f ? (new FormData(f)) : "");
xhttp.onreadystatechange = function () {
if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
var data = JSON.parse(xhttp.responseText);
g("x").innerHTML = data.x;
g("y").innerHTML = data.y;
xhttp = null;
g("status").innerHTML = "";
updateTime = 0;
}
}
}
return false; //prevent form redirect
}

window.onload = transmit();
transmit();
setInterval(function () { g("ut").innerHTML = ++updateTime; }, 1000);
setInterval(transmit, 5000); //autoupdate display every 5s
</script>

0 comments on commit 7183b65

Please sign in to comment.