javascript to refresh a web page during work hours

Mark Drelinger
Mark Drelinger used Ask the Experts™
on
javascript to reset html page during work hours.

I have this (cited from another post on Experts-Exchange) but it does nothing.

<script type="text/javascript">
//60000 milliseconds is 1 minute
window.setInterval("checkForRefresh()", 600);

function checkForRefresh() {
   var now = new Date();
   if (now.getHours() >= 7) {
    window.refresh();
   }
}
</script>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I'm using this snippet

function refreshAt(hours, minutes, seconds) {
    var now = new Date();
    var then = new Date();

    if(now.getHours() > hours ||
       (now.getHours() == hours && now.getMinutes() > minutes) ||
        now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {
        then.setDate(now.getDate() + 1);
    }
    then.setHours(hours);
    then.setMinutes(minutes);
    then.setSeconds(seconds);

    var timeout = (then.getTime() - now.getTime());
    setTimeout(function() { window.location.reload(true); }, timeout);
}

refreshAt(9,33,0); //Will refresh the page at 9:33

Open in new window

Analyst Developer
Distinguished Expert 2018
Commented:
With the current code if you're using 60000 milliseconds your code will remain refreshing from 08:00 until 23:59 but you need to use :

location.reload();

Open in new window


Instead of :

window.refresh();

Open in new window

Author

Commented:
That worked to refresh at a specific time, but not during work hours.  and since the refreshAt is set specifically, I don't see how it can be altered accordingly ?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
javascript to reset html page during work hours.

The description in your question does not help much, Please try to describe to us your final goal, what you want to achieve exactly so we could help more.
You need to refresh to which frequency?

Author

Commented:
Yes, I should have been clearer. I want the page to refresh every x seconds, but only drying work hours, specifically 8am to 5pm.
Page to stay idle between non-work hours.

Author

Commented:
refresh every 10 minutes.

Author

Commented:
This worked. Thanks to all

//60000 milliseconds is 1 minute
window.setInterval("checkForRefresh()", 60000);

function checkForRefresh() {
   var now = new Date();
   if (now.getHours() >= 7 && now.getHours() <= 18) {
    location.reload();
   }
}

Author

Commented:
Thanks all.  
Excellent code
//60000 milliseconds is 1 minute
window.setInterval("checkForRefresh()", 60000);

function checkForRefresh() {
   var now = new Date();
   if (now.getHours() >= 7 && now.getHours() <= 18) {
    location.reload();
   }
}
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
Then you need just to add a condition to your current one like :

if (now.getHours() > 7 && now.getHours() < 17) {
    window.refresh();
 }

Open in new window

Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018
Commented:
Good job you find it.

specifically 8am to 5pm.

Just as a side note , your current condition :
if (now.getHours() >= 7 && now.getHours() <= 18) {

Open in new window


Will include 7am and 6pm , be careful. If you want to exclude them check my previous comment...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial