HTML - check day/time but refresh page during working hours only

Viewed 108

The below script works well but I am looking to add the ability to refresh the index.htm page as part of the script and not the meta refresh tag. My "open" hours are Monday to Friday, 8am to 5pm.

It refreshes every 1 minute after page load but I want it to refresh every minute only during the "open" hours and not outside of it. However, if a visitor opens the index.htm page on a Monday at 7:59 am, is there should be a way to force page reload once it is 8:00 am. Would appreciate your help.

<meta http-equiv="refresh" content="60;url=index.htm">
=======
<script type="text/javascript">
var view = [
    'open-menu.png', 
    'closed-menu.png'
]; 
onload = function () {
  var now = new Date();
  var dayofweek = now.getDay();
  var HH = now.getHours();
  if ( ((HH >= 8) && (HH < 17)) && ((dayofweek >= 1) && (dayofweek <= 5)) ) {
    document.getElementById('image').src = view[0];
  } else {
    document.getElementById('image').src = view[1];
  }
}
</script>
========
<body>
<img id="image" src="">
</body>
2 Answers

If I understood correctly your demands, try this:

<script type="text/javascript">
var view = [
    'open-menu.png', 
    'closed-menu.png'
]; 
onload = function () {
  var now = new Date();
  var dayofweek = now.getDay();
  var HH = now.getHours();
  if ( ((HH >= 8) && (HH < 17)) && ((dayofweek >= 1) && (dayofweek <= 5)) ) {
    document.getElementById('image').src = view[0];
    window.setTimeout(function () {
       window.location.reload();
    }, 60000);
  } else {
    document.getElementById('image').src = view[1];
  }
}
</script>

Reloading the whole page is not the best solution in my opinion, try something like this:

setInterval(yourFunction, 60000)
Related