• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Java script window.addEventListener

Q&A Forum › Category: Questions › Java script window.addEventListener
0 Vote Up Vote Down
Glen Brackett asked 4 years ago

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();
}

3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Glen.
It seems fine to me. We use a similar approach in our projects.
Is there anything that is not working?
Regards,
Sara

0 Vote Up Vote Down
Glen Brackett answered 4 years ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

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

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.