We help IT Professionals succeed at work.

javascript to refresh a web page during work hours

Mark Drelinger
on
70 Views
1 Endorsement
Last Modified: 2018-12-06
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

CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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 ?
Zakaria AcharkiAnalyst Developer
CERTIFIED EXPERT
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.
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions