How to reload a page at a specific hour and only once a day

I have a javascript function that runs every minute.

setInterval(function(){ //first, check time, if it is 9 AM, reload the page var now = new Date(); if (now.getHours() == 9 { window.refresh(); } //do other stuff },60000); 

Now the problem is that the reboot occurs only once a day. since the function runs every minute, so the next time it lights up, it will reload the page again if it is between 9:00 and 10:00. How to reboot only once?

Perhaps I will do this by creating another interval function that fires every hour and checks if it should be rebooted. but since I already have a function that works every minute, can I do it from there?

If I end up creating another function that checks every hour. What happens if these 2 functions work at the same time?

+4
source share
3 answers

I would save the date of the last update, calculate the difference, and it is less than 6 hours (to be safe) you do not need an update.

 var lastRefresh = new Date(); // If the user just loaded the page you don't want to refresh either setInterval(function(){ //first, check time, if it is 9 AM, reload the page var now = new Date(); if (now.getHours() == 9 && new Date() - lastRefresh > 1000 * 60 * 60 * 6) { // If it is between 9 and ten AND the last refresh was longer ago than 6 hours refresh the page. location.reload(); } //do other stuff },60000); 

Hope this is what you had in mind. Please note: this is not verified.

+5
source

without going into more complicated decisions in order to make it better, you can use getDay () and save it in a cookie so that you can check if this method was last called the same day, this way only a day after of how you will be at 9 in the morning and another day.

+1
source

It is in C #. The algorithm will be:

  • Create a timer in C # where it works at the N-th hour (for example, 10:00, 14:00, etc.).
  • You check if the time is X-hour or not (in this case, 9:00 AM).
  • If so, refresh the page.
-2
source

All Articles