5

Long time browser, first time asker.

I'm starting an online store that updates it's homepage every night at midnight EST. I need the transition to happen smoothly and have the site refresh automatically when the clock strikes 12 for users on the page. Similar to to sites like teefury.com and theyetee.com

Is there a script that can achieve this?

Thanks so much!

Dennis Menzel
  • 73
  • 1
  • 8
  • 1
    But you realize that users can circumvent this by setting their clock appropriately? Is that something you can live with? – Pekka Feb 02 '14 at 15:49
  • 1
    possible duplicate of [JavaScript countdown timer: Calculate how many seconds until midnight EST](http://stackoverflow.com/questions/21482562/javascript-countdown-timer-calculate-how-many-seconds-until-midnight-est) – Joeytje50 Feb 02 '14 at 15:51
  • I would suggest better than just refreshing the page on midnight, to be polling for updates every minute, so doesn't really mather whenever you update it or sincrhonizing anything. But also, when user clicks anything, before sending the response, check if it still available and if not, return a customized page error saying that the page is no longer active or something – Fernando Rybka Feb 02 '14 at 16:00

1 Answers1

5

Like the similar question https://stackoverflow.com/a/21482718/1256925, you can do this the following way:

var now = new Date();
var night = new Date(
    now.getFullYear(),
    now.getMonth(),
    now.getDate() + 1, // the next day, ...
    0, 0, 0 // ...at 00:00:00 hours
);
var msTillMidnight = night.getTime() - now.getTime();
setTimeout('document.location.refresh()', msTillMidnight);

The only difference here is the final timer that runs a script. The rest of the script works the same as in that other question.

You can change the value for var night to the following if you want to use UTC instead of the user's local time:

var night = new Date(
    now.getUTCFullYear(),
    now.getUTCMonth(),
    now.getUTCDate() + 1, // the next day, ...
    0,
    now.getTimezoneOffset(), //this returns the amount of minutes difference in timezones
    0
);
Community
  • 1
  • 1
Joeytje50
  • 18,636
  • 15
  • 63
  • 95
  • Now, @Joeytje50, does this timer work off of the server clock or the users clock? Also do you know a way for the html to update automatically at 12? – Dennis Menzel Feb 02 '14 at 16:45
  • @user3263100 this new code will work for 'server time', if your server runs using UTC (which it should). Also, what do you mean by 'update automatically at 12'? This script refreshes the page at 0:00:00h. Is that not what you wanted? Just change the first `0` to `12` if you also want it to refresh at noon. – Joeytje50 Feb 02 '14 at 17:53
  • Oh no, this is exactly what I need! But i was wondering if there's a way for the code itself to change directly at midnight. For example, I'm doing a 24 hour sale and I want it to be taken off the home page directly at 12 midnight. Is there anyway to update the homepage precisely at 12 without actually manually changing all the code by hand? @Joeytje50 – Dennis Menzel Feb 02 '14 at 20:09
  • @user3263100 You mean you'd want to modify the actual code the server sends back? For that, you'd probably need to ask a seperate question here on StackOverflow, and ask others. I wouldn't know how to do that, but at least now you know how to refresh the page at exactly midnight. If this was indeed what answers this part of your question, please mark this question as answered via the checkmark to the left of my answer. – Joeytje50 Feb 02 '14 at 20:36
  • 1
    What if `now.getDate() + 1, // the next day` get's the `32`? Maybe better use a library like momentjs.com for those things in Javascript, it's less headache. – Martin Schilliger Jul 19 '16 at 12:48
  • 1
    @MartinSchilliger Try this in your console: `new Date(2016,0,32)`. That should return January the 32nd, but JavaScript understands that this means it's February the 1st, so that's what it uses. This script works just fine without the requirement to install another library. – Joeytje50 Jul 19 '16 at 23:59