Local Weather: How to udpate weather every hour


#1

I am confused on how to update the weather obtained through the api every hour. Here is the codepen: https://codepen.io/Ag_Yog/pen/wqgzNE. I wrote the following code in the attempt to update the code within my getLocation() function:
setTimeout(getLocation, 1800000);

Thanks for any help


#2

If you want a function to run at regular intervals, use setInterval() instead of setTimeout().

setInterval(myFunction, 1000); // runs myFunction() every second
setTimeout(myFunction, 1000); // run myFunction() once after 1 second passes 
  • 1 second = 1000 milliseconds
  • 1 minute = 60 seconds = 60000 milliseconds
  • 1hr = 60 minutes = 3600 seconds = 3600000 milliseconds

So, to run your getLocation function once an hour, you would do:

setInterval(getLocation, 3600000);

(You could also write that number in scientific notation to make it easier to see how many zeroes there are: 36e5.)


Before you can get the above working, however…you will need to wrap all of your code in a single function. Right now, your getLocation() function requires data from navigator.geolocation, so running getLocation() by itself without any arguments results in failure.