How do I refresh a page on a specific day at a specific time in a specific time zone?

I am developing a website that has two webcasts. I used PHP to display the time when buttons should be turned on and off. Now I need to use Javascript to automatically refresh the page before the first broadcast, before the second broadcast and after the second broadcast. I have implemented the following script, and it refreshes the page at a given time, however it does not work exactly as I need it. I need to change the script so that it only refreshes the page on Sundays at 7:45 p.m., 8 p.m. and 8:30 p.m. only EST.

I am using a modified script answer to this question

function refreshAt(hours, minutes, seconds) { var now = new Date(); var then = new Date(); if(now.getUTCHours() > hours || (now.getUTCHours() == hours && now.getUTCMinutes() > minutes) || now.getUTCHours() == hours && now.getUTCMinutes() == minutes && now.getUTCSeconds() >= seconds) { then.setUTCDate(now.getUTCDate() + 1); } then.setUTCHours(hours); then.setUTCMinutes(minutes); then.setUTCSeconds(seconds); var timeout = (then.getTime() - now.getTime()); setTimeout(function() { window.location.reload(true); }, timeout); } 

Then I call the refreshAt () function.

 refreshAt(19,45,0); //Will refresh the page at 7:45pm refreshAt(20,00,0); //Will refresh the page at 8:00pm refreshAt(20,30,0); //Will refresh the page at 8:30pm 

Where am I confused how to change this script only to update update for EST on Sundays.

+4
source share
5 answers

I get it. Here's the script:

 function refreshAt(hours, minutes, seconds, day) { var now = new Date(); var then = new Date(); var dayUTC = new Date(); if(dayUTC.getUTCDay() == day) { if(now.getUTCHours() > hours || (now.getUTCHours() == hours && now.getUTCMinutes() > minutes) || now.getUTCHours() == hours && now.getUTCMinutes() == minutes && now.getUTCSeconds() >= seconds) { then.setUTCDate(now.getUTCDate() + 1); } then.setUTCHours(hours); then.setUTCMinutes(minutes); then.setUTCSeconds(seconds); var timeout = (then.getTime() - now.getTime()); setTimeout(function() { window.location.reload(true); }, timeout); } } 

And then I just call the refreshAt () function:

  refreshAt(20,00,0,1); //Will refresh the page at 8:00pm on Monday UTC or 3:00pm EST 
+4
source

You can add a test even during the day of the week, for example:

 now.getDay() == "Sunday" 
+1
source

An alternative approach would be to use Pusher . This provides an open connection to receive events.

Enable javascript for Pusher:

 <script src="http://js.pusher.com/1.11/pusher.min.js"></script> 

Bind to the pusher event "update" with this code:

 var pusher = new Pusher('abc123'); // Get a key when you sign up and replace this var refreshChannel = pusher.subscribe('refreshing'); refreshChannel.bind('refresh', function(thing) { location.reload(true); }); 

Then, at 8pm (or whenever you like) manually display the pusher event on the Pusher dashboard page, and all people viewing the page will be reloaded.

+1
source
 function refreshAt(day, hours, minutes, seconds) { Date.getDay() = day ... } 

0 - Sunday, 6 - Saturday.

+1
source

I had the same problem as you. I am creating a site that is very similar to yours, also using PHP to enable and disable page elements before and after airtime. Your solution seemed promising, but in the end I was unhappy with using pure Javascript to reload the page. Javascript gets time from the client machine, while PHP gets it from the server, and even a small difference between them can destroy the whole system. (for example, a page might refresh 30 seconds before PHP turns on the buttons, leading some viewers to assume that all this does not work.)

I solved the problem using PHP to tell Javascript functions what time it was happening and it works like a charm. The only problem is that it works every day, and not just on one day of the week, but it does not bother me - users will have no reason to view the page except for the broadcast day.

That's all you need - I put this right after the <body> :

 <script type="text/javascript"> setTimeout(function() { window.location.reload(true); }, 1000*<?php $then = mktime(15,00,0); $tomorrow = mktime(15, 00, 0, date("m") , date("d")+1); $now = time(); if ($now > $then) {echo $tomorrow - $now;} else {echo $then - $now;}?>); </script> 
+1
source

Source: https://habr.com/ru/post/1389932/


All Articles