Storing and managing API JSON responses

So, I’m currently making Weather App project and I encountered a problem. Openweathermap suggests that you should make an API call not more than every 2 hours, because weather doesn’t usually change so quickly. So I came up with an idea:

  • When you load a page, something checks when was your last API call and if it was earlier than 2 hours ago, it would automatically send a request again
  • There would be a “refresh” button that would make a request when you press it.
    The problem is - how should I do it? I thought about using cookies but I don’t really know much about them, whether they have some kind of timestamp etc. Thanks for help.

You’re just thinking about storing the history for that individual user, right?

If it were me, I’d probably look at using local storage. What you could do is create an object that stores the weather information and the current time and store that. (You’d have to store it as a string - use JSON.stringify to turn it into a string and JSON.parse to turn it back into an object).

You can create your own “timestamp” using the JavaScript date object (Date.now() will give you the current time). When you’re getting ready to make an API call, just use Date.now() to get the current time and compare it to the time of the last call that you made.