0

How would i go about a AUTO refresh code in Java or HTML that refreshes every 3 hrs and 45 min? Not like per user like even if its a user has been on the page only 10 min if its been 3 hours and 45 min since the last Refresh it will refresh anyway so like 3 hours and 45 min Server time not User time if that makes sense like 12:45 am 3:45 am 6:45 am 9:45 am 12:45 pm 3:45 Pm... Soo on soo on Hope this makes sense thanks for your help guys!

i have tried the following but it goes off user time

<meta http-equiv="refresh" content="13500" >
  • 1
    I'd recommend setting up [websockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) or [server-sent](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) events. – Phix Jun 27 '18 at 21:37
  • Im using shared hosting with cpanel i really need a javascript if i can –  Jun 27 '18 at 21:39
  • it could help you https://stackoverflow.com/questions/12038183/refresh-page-for-interval-using-js – Juan Caicedo Jun 27 '18 at 21:40
  • that goes by user time and not server time tho dont it? Like if the user has only been there 10 min it wont refresh until they are on the page 3 hours and 35 more min. time i think –  Jun 27 '18 at 21:43
  • How flexible is the 3 hour, 45 minute requirement? If it were a 3-hour refresh period, you could check every minute if it was 12:00 AM/PM, 3:00 AM/PM, 6:00 AM/PM or 9:00 AM/PM. Or if it were a 4-hour refresh period, you could simply check every minute for 12:00 AM/PM, 4:00 AM/PM or 8:00 AM/PM – Chris Forrence Jun 27 '18 at 21:44
  • Have the server inject the proper timeout value and it'll be fine... – Chris Jun 27 '18 at 21:45
  • i got a token that expires every 4 hours but the token up dates at 3 hrs and 30 min so i need the page to refresh about 3 hrs 40- 3 hr 45 minish so its updated before the 4 hour mark and kills everything –  Jun 27 '18 at 21:46

2 Answers2

1

You can do this in plain JavaScript.

<script type="text/javascript">
setTimeout(function(){
    //You can change this value based on the server location.
    var offset = -5;
    var now = new Date();
    var utc = now.getTime() + (now.getTimezoneOffset() * 60000);
    var serverTime = new Date(utc + (3600000*offset));
    var hours = serverTime.getHours();
    var minutes = serverTime.getMinutes();
    if((hours % 3 == 0) && (minutes % 45 == 0)){
        location.reload();
    }
}, 30000);
</script>

You can include this script in your page and it will reload every three hours. The setTimeout is set to call every 30000ms or every 30 seconds.

Edit: Added offset value for handling multiple server time zones. You can refer to this link for exact values.

Thanks @ChrisForrence for the suggestion.

Raghuram Kasyap
  • 305
  • 2
  • 9
  • @Raghuram Kasyap after testing this dont work.. your code for every 3 hours only works for certain people thats actually in the -5 timezone everyone else dont get refreshed –  Jun 28 '18 at 14:55
  • I see that you cannot use chat rooms. I've uploaded a working [sample on codepen](https://codepen.io/anon/pen/dKQeGP?editors=1111) – Raghuram Kasyap Jun 28 '18 at 15:54
0

Your code will refresh the webpage based on the server time.

To fix the issue you need to use a timer and add function using javascript and based on it you can start the timer when you access the page and after every regular interval, you can refresh.

Use this URl for sample code snippet : http://www.javascriptkit.com/script/script2/autofresh.shtml

var limit="****" // time like "225:00" minutes
    function beginrefresh(){
        if (parselimit==1)
            window.location.reload()
        else{ 
            parselimit-=1
            curmin=Math.floor(parselimit/60)
            cursec=parselimit%60
            if (curmin!=0)
                curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
            else
                curtime=cursec+" seconds left until page refresh!"
            document.title = doctitle + ' (' + curtime +')'
            setTimeout("beginrefresh()",1000)
        }
    }

Hope this helps.

LearningToCode
  • 392
  • 5
  • 18