Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

I have a javascript function that runs every one 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, I want the reload to happen only once a day. since the function runs every minutes, so next time it fires up, it will reload the page again if it is between 9AM and 10AM. How do I make the reload happen only once?

I can probably do it by creating another interval function that fires every hour and check if I should reload. but since I already have the above function that runs every minute, can I do it from there?

If I do end up with creating another function that checks every hour. What will happen if those 2 functions fire up at the exact same time?

like image 538
neo Avatar asked Feb 22 '13 19:02

neo


People also ask

How do you refresh a page every 5 minutes?

Search for "Tab Reloader (page auto refresh)" in Google. With tab reloader, you can set times for each tab to reload individually. For example, you can set your tab to eBay to reload every 10 seconds and your YouTube tab to reload every 5 minutes.

How do I refresh a web page every 5 seconds?

Open the web page that you want to automatically refresh at certain seconds of the interval. Then, click on the extension icon in your Chrome bar and select the interval time.

What is window location reload?

Window location. The reload() method reloads the current document. The reload() method does the same as the reload button in your browser.


2 Answers

I would store the date of the last refresh, calculate the difference, and it it's less than 6 hours (to be safe) you won't need a refresh.

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

I hope this is what you meant. Note this is untested.

like image 74
11684 Avatar answered Sep 19 '22 22:09

11684


without going into more complex solutions for getting this done in a nicer way, you can use getDay() and save it in a cookie so that you can check if the last time this method was called it was with the same day, this way only a day after you'll be at 9am and in a different day.

like image 40
TheZuck Avatar answered Sep 22 '22 22:09

TheZuck