setInterval() with addEventListener event

Having problems with the addEventListener. If I skip that and just use setInterval(get_iss, 2000) it will cycle every two seconds but I want to use the addEventlistener.

async function getIss() {
  // make request
  const response = await fetch('https://api.wheretheiss.at/v1/satellites/25544');
  const data = await response.json();
  // update UI
  document.getElementById("altitude").textContent = data.altitude;
  document.getElementById("lat").textContent = data.latitude;
  document.getElementById("lng").textContent = data.longitude;
}

document.addEventListener("DOMContentLoaded", function(event) {
  console.log("Starting requesting data...");
  // load data once
  getIss()
  // define interval
  const seconds = 2;
  // execute the function every x seconds (safe interval ID so we can stop the interval later)
  const interval = setInterval(getIss, seconds * 1000)

  // stop after 10 seconds here (just to not go overboard with requests here, you might not need this)
  setTimeout(() => {
    clearInterval(interval);
    console.log("Stopped requesting data.")
  }, 10 * 1000)
});
1 Like

You got:

1. Document.addEventListener()

3. seconds = 2

2. Async getIss()

4. Interval = setInterval( getIss, seconds * 11)

5. SetTimeout()

have a function that deals with ur async method and then have ur UI elements in another function

It did not like the async either.

Yeah I hope this picture helps, keep track of those web APIs aka async

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.