2

I would like my homepage to change each day at a specific time (1pm).

The page has a 24hr countdown timer and when it reaches zero, I would like a new page to load and the timer starts again.

I understand how to make a page refresh after a particular time

<script>
    setTimeout(function(){
       window.location='Page2.html';
    }, 5000);
</script>

But not how to make this happen at a particular time of the day (1pm).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Paul Hesketh
  • 95
  • 2
  • 10
  • Is it the page content you wish to change? Does the page content come from a db? – Professor Abronsius Sep 11 '16 at 07:07
  • Try learning about cron jobs – muya.dev Sep 11 '16 at 07:08
  • As the site develops, I will build a SQL database, but at present, I design and upload the new page for each day. – Paul Hesketh Sep 11 '16 at 07:11
  • Cron jobs won't really help unless he is using web sockets since the server can't push data via http – Dan Sep 11 '16 at 07:11
  • I think this [link](http://stackoverflow.com/questions/4455282/call-a-javascript-function-at-a-specific-time-of-day) may help you out. – rcr Sep 11 '16 at 07:17
  • If you have 24hr timer coundown, so just call your window.location='Page2.html'; after countdown is 0... There the coundown time came from? is from server or you create is locally? – freethinker Sep 11 '16 at 07:27

5 Answers5

5

You can try using a getting the current time on page load/refresh. Then calc the milliseconds until 1pm. And use that to set your setTimeout. I suggest using a library like moment to do time calculations.

Load moments in your html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script>

In JS:

// time right now
var now = moment.now();
// set refresh hour to 1pm
var nextRefresh = moment.now().hour(13).minute(0).second(0).millisecond(0);

// check if is or after 1pm
if (now.hour >= 13) {
  nextRefresh.add(1, 'days'); // add 1 day
}

setTimeout(function() {
  console.log('next 1pm');
}, nextRefresh.diff(now));

And @Stoycho Trenchev is right. You will probably want to call setInterval with 86400000 ms in the setTimeout. This way, your page will refresh everyday afterwards.

Dan
  • 506
  • 5
  • 18
  • I think you just went over OP's head. Also this will get stuck in a endless browser refresh unless your storing nextRefresh in localStorage. – Darkrum Sep 11 '16 at 07:39
  • Honestly, I think its simpler than using JS date and having logical operators. The code is very readable. Only thing is loading the moment library but that shouldn't be too bad. And good catch on the endless refresh, I flipped the diff order. Answer is updated. – Dan Sep 11 '16 at 07:49
  • No argument about JavaScript's native time api sucking hard. If it wasn't for moment and their timezone library i would have jumped out a building. – Darkrum Sep 11 '16 at 08:05
2

You need setInterval not setTimeout and you need to calculate 24h in milliseconds :)

Stoycho Trenchev
  • 557
  • 4
  • 12
2

Here you go just a fyi JavaScript uses the browsers time so just because it's 1pm where you are it won't be 1pm where the user is.

var intervalId = window.setInterval(checkTime, 500);

function checkTime() {

    var d = new Date();
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();

    if(h == 13 && m == 0 && s == 0) return window.location='Page2.html';
}
Darkrum
  • 1,325
  • 1
  • 10
  • 27
1

Ah. Something like?

<script>
    function getTime() {
        var date    = new Date()
        var time    = date.getTime();
        var hours   = time.getHours();
        var minutes = time.getMinutes();
        var seconds = time.getSeconds();

        var time    = {'hours': hours, 'minutes': minutes, 'seconds': seconds};
    }

    setInterval(function() {
        var time    = getTime();

        if (time.hours === 13 && time.minutes === 0) {
             window.location = 'Page2.html';
        }
    }, 500);
</script>
1

You'll need setTimeout to set a timer and Date to calculate how long the timer needs to go until it triggers.

var now = new Date();
var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now;
if (millisTill10 < 0) {
     millisTill10 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function(){alert("It's 10am!")}, millisTill10);
rcr
  • 142
  • 1
  • 2
  • 11