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?
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.
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.
Window location. The reload() method reloads the current document. The reload() method does the same as the reload button in your browser.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With