I’m not familiar with Ajax so I’m learning fast. I’d appreciate some help on displaying a variable
rdm = urandom.randint(10,100) //comes from a loop in micropython.
Here’s what I’ve so far. The part of turning ON/OFF a LED is working. I just cannot get the variable to update every 2 seconds.
TIA
<html>
<head>
<title>ESP32-OLED
</title>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.js" type="text/javascript">
</script>
</head>
<body>
<div>
<h4>The ESP32 Update web page without refresh
</h4>
<br>
<p>LED State:
<strong>%s
</strong>
</p>
<p>RDM number:
<span id="rdm">0
</span>
</p>
<p>
<button onclick="window.location.href = '/?led=on'">ON
</button>
</p>
<p>
<button onclick="window.location.href = '/?led=off'">OFF
</button>
</p>
<br>
<a href="http://www.google.com">www.google.com
</a>
</div>
<script>
var txt = "jQuery Works";
var _rdm = rdm;
//wait for the page to fully load
//$(document).ready(function(){
// var txt = "jQuery Works";
// alert(txt)
//}
//);
setInterval(function() {
// Call a function repetatively with 2 Second interval
getData();
}, 2000);
//2000mSeconds update rate
function getData() {
var xhttp = new XMLHttpRequest();
var rdm = document.getElementById("rdm").value;
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//returns 4 when the server finishes streaming the response
document.getElementById("rdm").innerHTML = _rdm;
//return object whose id property matches the specified string
}
};
xhttp.send();
}
</script>
</body>
</html>```
2
Answers
You should resolve xhr before calling interval/timeout, also you need to call method
open