How to automatically reload a web page at a certain time?

I have a website that I want to be reloaded at a certain time, like 3:35pm, not after a specific interval like 5min. How do I do that?

Answers:

Answer
<META HTTP-EQUIV="Refresh" CONTENT="5">

this will force page to reload every 5 seconds. Just calculate the correct interval and add it to content tag

Answer

I found this page with a similar question and used it to hack out a more specific answer that may be of use to some. For this project, we wanted to make sure that the page refreshed once a live event of global interest was about to go on, activating the player embed on the user's page (narrow use case, I know -- others might have a better use for it).

One challenge in the above answers was how to deal with time zone conversions, which was more of an issue for us because we wanted to make sure that the page refreshed at a specific day and time. To do this, I grabbed a UTC version of the target date and today's date, converted them to GMT, then set Andrew's timeout function to the difference between the two.


var target = new Date("January 28, 2011 13:25:00");
timeOffset = target.getTimezoneOffset() * 60000;
targetTime = target.getTime();
targetUTC = targetTime + timeOffset;

var today = new Date();
todayTime = today.getTime();
todayUTC = todayTime + timeOffset;

refreshTime = (targetUTC - todayUTC);
if (refreshTime > 1) {
    setTimeout(function() { window.location.reload(true); }, refreshTime);
}
Answer

Basically, there are many javascript codes out there that can refresh the page ever so minutes or something, you can edit them to refresh at hours too. Like this one:

//enter refresh time in "minutes:seconds" Minutes: 0 to Whatever
//Seconds should range from 0 to 59
var limit = "0:30";

if (document.images) {
    var parselimit = limit.split(":");
    parselimit = parselimit[0] * 60 + parselimit[1] * 1;
}
var beginrefresh = function () {
    if (!document.images) return 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!";
        window.status = curtime;
        setTimeout("beginrefresh()", 1000);
    }
}

window.onload = beginrefresh;

(now just calculate the minutes and seconds you want it to refresh, like for example noon everyday if it were noon now:

var limit = "1440:00";

Now you could use this code except, most of them don't work with server time, And with the information you provided us, we really can't do anything more. Edit your question and tell us if you want it to be timed with the servers time, or something else.

Answer

I hope this help,you can set the exact time for refresh

var target = new Date("November 18, 2019 10:00:00");
timeOffset = target.getTimezoneOffset() * 60000;
targetTime = target.getTime();
targetUTC = targetTime + timeOffset;

var today = new Date();
todayTime = today.getTime();
todayUTC = todayTime + timeOffset;

refreshTime = (targetUTC - todayUTC);
if (refreshTime > 1) {
setTimeout(function() { window.location.reload(true); }, refreshTime);
}
Answer

Use this to refresh the page every 20 seconds.

<META HTTP-EQUIV="refresh" CONTENT="20">
Answer

The following JavaScript snippet will allow you to refresh at a given time:

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);
}

Then you can add a script tag to call the refreshAt() function.

refreshAt(15,35,0); //Will refresh the page at 3:35pm
Answer

Basically, when the page is accessed, calculate how much time is remaining between the access time and the time you want to reload the page, and use that remaining time in the meta refresh header. Obviously this would need to be done in a CGI script or web application, or possibly with SSI (server-side includes); it won't work if all you have is a static HTML file.

Another alternative would be to use Javascript, but it won't work if the client has Javascript disabled.

Answer

This worked better for my purposes.

If you're able to use Jquery and MomentJs, you can do this:

(function () {
    var $ = require('jquery');
    var moment = require('moment');

    function refreshPageAtTime(expiration, countdownElement) {
        var now = moment.utc();
        console.log('now', now);
        var expirationMoment = moment.utc(expiration, 'YYYY-MM-DD kk:mm:ss');
        console.log('target', expirationMoment);
        var secondsUntilRefresh = expirationMoment.diff(now, 'seconds');//http://momentjs.com/docs/#/displaying/difference/
        console.log('diff in seconds', secondsUntilRefresh);
        if (secondsUntilRefresh > 1) {
            setInterval(function () {
                secondsUntilRefresh--;
                console.log('seconds remaining', secondsUntilRefresh, 'seconds');
                if (secondsUntilRefresh <= 10) {
                    countdownElement.html(secondsUntilRefresh + '...');
                    if (secondsUntilRefresh === 0) {
                        console.warn('Refreshing now at ' + moment.utc());
                        window.location.reload(true);
                    }
                }
            }, 1000 * 1);
        }
    }

    $(document).ready(function () {
        var expiration = $('form').attr('data-expiration');
        console.log('expiration', expiration);
        $('.btn-primary:submit').after('<div id="countdownToRefresh" style="display: inline-block; color: #999; padding: 10px;"></div>');
        refreshPageAtTime(expiration, $('#countdownToRefresh'));

    });
})();

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.