Main Content

Updating a page strictly every minute (PHP, Perl)

Archive - Originally posted on "The Horse's Mouth" - 2007-05-14 14:11:39 - Graham Ellis

For the Clock exercise that I mentioned in my last entry, I need to update the page once a minute. Exactly once a minute. At the start of the minute.

I'm using client pull, where the browser asks for the next page - so my basic request is:
<meta http-equiv="refresh" content="60; url=/clock/now.php">
but that will neither refresh at the start of the minute, nor be exact - with update times and delays taken into account, a handful of times in each hour will be missed out (I know this - I tried it) as the 60 seconds starts from the time the new page is received, not from the previous page.

So I have adjusted the time as follows:
$togo = 61 - date("s");
if ($togo < 15) $togo = 15;

to calculate how long before we trigger - it's going to trigger 1 second into the next minute, and will be selfcorrecting to allow for page transmission times. The business with the "15" is to ensure that initial pages display for at least 15 seconds, even if - by rights - they should be refreshed quicker. That will help avoid an irritating refresh. And the client pull instruction becomes:
<meta http-equiv="refresh" content="<?= $togo ?>; url=/clock/now.php">



The example above is "Client Pull". There's also a complete "Server Push" example, written in Perl, using the same algorithm available for you here