Link to home
Create AccountLog in
Avatar of Mark Drelinger
Mark DrelingerFlag for United States of America

asked on

javascript to refresh a web page during work hours

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>
Avatar of lenamtl
lenamtl
Flag of Canada image

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

ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Mark Drelinger

ASKER

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 ?
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?
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.
refresh every 10 minutes.
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();
   }
}
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();
   }
}
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

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account