Avatar of Mark Drelinger
Mark Drelinger
Flag 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>
HTMLJavaScript

Avatar of undefined
Last Comment
Zakaria Acharki

8/22/2022 - Mon
lenamtl

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
Zakaria Acharki

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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 ?
Zakaria Acharki

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
lenamtl

You need to refresh to which frequency?
Mark Drelinger

ASKER
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.
Mark Drelinger

ASKER
refresh every 10 minutes.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mark Drelinger

ASKER
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();
   }
}
Mark Drelinger

ASKER
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 Acharki

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.