Is this the correct way to use the addEventListener. When the web page loads, I’m trying to ‘fill in’ the data from different sources. I have 3 different functions, getTime, getChipInfo and getDHTdata that area all similar to the one shown. Ive tried searching for examples but could not find anything meaningful.
Thanks for any help. Glen
// Get current sensor readings when the page loads
window.addEventListener(‘load’, loadPageData);
Function loadPageData() {
getTime();
getChipInfo();
getDHTdata();
}
// Function to get current readings on the webpage when it loads for the first time
function getTime(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
console.log(myObj);
document.getElementById(“current_time”).innerHTML = myObj.time;
}
};
xhr.open(“GET”, “/time”, true);
xhr.send();
}
Hi Glen.
It seems fine to me. We use a similar approach in our projects.
Is there anything that is not working?
Regards,
Sara
Thank you for response. Initially I was sending all the data every time and was trying to figure out how to send data only on when the page loads or when requested. My first attempts were causing a hard reset. That has since been corrected.
Thank you again for your tutorials and books. I’m constantly returning to both as I learn and develop more with ESP and Raspberry Pi. They have most helpful.
Glen
That’s great!
Alternatively, you can send a simple request that resets the delay time. So, the ESP thinks it’s time to send new readings on the loop() via SSE.
I’m glad you’re enjoying our eBook.
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara