Jump to content

how to refresh content every minute (accoring to the clock)


alapimba

Recommended Posts

Hello

 

I need to have 3 computers refreshing content every minute at the exact same time.

 

how can i do that? i don't want to use the tag <meta http-equiv="refresh" content="60; URL=http://www.yourdomain.com/yoursite.html"> because it will not refresh syncronized with the computer clock and has i'll have 3 computers side by side it has to be at the exact same time. so i thougth about the refresh be based with the computer clock.

 

 

Anyone can help?

Link to comment
Share on other sites

You don't need AJAX. You can just use JavaScript to force a refresh at every even 10 second interval (1:25:00, 1:25:10, 1:25:20, etc.). Although you have to keep in mind it always takes some length of time to refresh a page - it is not instantaneous - even if it is a few milliseconds. The more content on the page the longer it takes to load.

 

Here is a working script. Just put the script in the head of your page and call the function in the onload trigger of the body. Remember to take out the debug section when you put it on your actual page. The first page load may trigger a refresh anywhere from 0-10 seconds in order to sync up with the even 10 second intervals.

<html>
<head>
<script type="text/JavaScript">
function autoRefresh()
{
    //Create timestamp upon load
    var loadTS = new Date();
    //Calculate milliseconds to next even 10 second interval
    var millisecondsToRefresh = 10000 - ((loadTS.getSeconds()%10*1000) + loadTS.getMilliseconds());
    // ##### BEGIN DEBUG CODE #####
    document.getElementById('loadtime').innerHTML = loadTS;
    document.getElementById('loadmilliseconds').innerHTML = loadTS.getMilliseconds();
    document.getElementById('refreshseconds').innerHTML = Math.floor(millisecondsToRefresh/1000);
    document.getElementById('refreshmilliseconds').innerHTML = millisecondsToRefresh%1000;
    // ##### END DEBUG CODE #####
    //Set page refresh
    setTimeout("location.reload(true);", millisecondsToRefresh);
}
</script>
<title>PHPMIND – Javascript Tutorials</title></head>
 
<body onload="autoRefresh();">
    <h2>JavaScript Auto Refresh Page</h2><br>
    <p>This page will be automatically refreshed at every even 10 second interval.<p><br>
    <p>Page was loaded at: <span id="loadtime"></span> and <span id="loadmilliseconds"></span> milliseconds.</p><br>
    <p>It will be refreshed in <span id="refreshseconds"></span> seconds and <span id="refreshmilliseconds"></span> milliseconds.</p>
</body>
</html>
Edited by Psycho
Link to comment
Share on other sites

I think it will take a bit more than that. It seems he want to use the local time on each computer to refresh at certain times, not just intervals, so they will all retrieve the update at the same time (assuming all clocks are sync'd). Like 10:00:00am, 10:00:10am, 10:00:20am, etc.

Link to comment
Share on other sites

I think it will take a bit more than that. It seems he want to use the local time on each computer to refresh at certain times, not just intervals, so they will all retrieve the update at the same time (assuming all clocks are sync'd). Like 10:00:00am, 10:00:10am, 10:00:20am, etc.

 

Not sure I follow. The above script will update the page on 10 second intervals based on the users system time. I did misread the requirement to be 10 seconds as opposed 1 second. But,per the requirements, he wants to refresh based on the computer clock. The above script will do exactly that. If the clocks are not synced, there's nothing that JavaScript can do. Of course, we could set the refresh time on the server to make sure they will all be set exactly the same irrespective of the user's computer time. But, with only 1 second intervals, the transmission/load times would probably make it just as inaccurate.

 

Here is the code updated to only 1 second intervals to occur at exactly the 1 second mark based on the computer's clock. But, with 1 second intervals I question the value. Just running the page locally takes 15-30 milliseconds to refresh. If the page it being requested via the internet it will take considerably more. So, just forcing a refresh at the same time does not guarantee that each user will get the refreshed page at the same time.

 

 

<html>
<head>
<script type="text/JavaScript">
function autoRefresh()
{
    //Create timestamp upon load
    var loadTS = new Date();
    //Calculate milliseconds to next even 10 second interval
    var millisecondsToRefresh = 1000 - (loadTS.getMilliseconds()%1000);
    // ##### BEGIN DEBUG CODE #####
    document.getElementById('loadtime').innerHTML = loadTS;
    document.getElementById('loadmilliseconds').innerHTML = loadTS.getMilliseconds();
    document.getElementById('refreshmilliseconds').innerHTML = millisecondsToRefresh%1000;
    // ##### END DEBUG CODE #####
    //Set page refresh
    setTimeout("location.reload(true);", millisecondsToRefresh);
}
</script>
<title>PHPMIND – Javascript Tutorials</title></head>
 
<body onload="autoRefresh();">
    <h2>JavaScript Auto Refresh Page</h2><br>
    <p>This page will be automatically refreshed at every even 1 second interval.<p><br>
    <p>Page was loaded at: <span id="loadtime"></span> and <span id="loadmilliseconds"></span> milliseconds.</p><br>
    <p>It will be refreshed in <span id="refreshmilliseconds"></span> milliseconds.</p>
</body>
</html>
Link to comment
Share on other sites

I need to have 3 computers refreshing content every minute at the exact same time.

I was referring to "exact same time". Like it sounds like he wants all 3 computers to request the data at the same physical time, not just same interval of 10 seconds apart. Like PC 1 send the initial page request at 10:00:10, even if the page was initially accessed at 10:10:05, etc. PC 2 sends the initial request at 10:00:10 even if it was initially accessed at 10:00:01, etc.

 

Looking at the code, if all 3 browsers first accessed the page 2 seconds apart, then all of their requests would be 2 seconds apart (10 seconds in between request intervals though). I think there needs to be a check to delay the refresh the first time to make sure the time (in seconds) is evenly divisible by 10. If not, wait until it is.

 

Like if the current time is 10:00:05, it should wait until 10:00:10 until it starts the 10 second apart interval refreshes. That way, no matter what time the initial request is made from the separate 3 computers, they would still get updated at the same time and not just 10 seconds apart from initial page load.

 

Otherwise it seems they'd have to initially manually access the page from all 3 computers at the exact same physical time to have them refresh at the same physical time.

Link to comment
Share on other sites

I had another thought. I am assuming that the intent of this is that users will see the updated content at exactly the same time. And, perhaps, they will be competing against each other. So, not only would you want the users to see the content at the same time, we want to ensure that the first one to respond actually gets credit for responding first - again we can't control how fast any response is from or to any PC from the server.

 

If the above is (or is similar) to what you are trying to achieve, I probably would use AJAX and take the following approach:

 

Using a setTimeout() event as above to execute at exactly every event second:

 

1. Check if previous update content was retrieved. If so, output it to the page

2. Request new updated content

3. Repeat from #1

 

This way, if it takes one user 15 milliseconds to retrieve the content and another person 45 milliseconds, they will still see it at the exact same time (assuming their computer clocks are synced)

 

Secondly, I would implement a JavaScript submit function that would populate a hidden field with a timestamp and then submit the form. This would ensure that each user is credited with the exact time they submitted their response. And, on the server-side you would have to make sure each response is saved with that timestamp and there would need to be some delay (a couple seconds) before actually reporting who responded first.

Link to comment
Share on other sites

I was referring to "exact same time". Like it sounds like he wants all 3 computers to request the data at the same physical time, not just same interval of 10 seconds apart. Like PC 1 send the initial page request at 10:00:10, even if the page was initially accessed at 10:10:05, etc. PC 2 sends the initial request at 10:00:10 even if it was initially accessed at 10:00:01, etc.

 

Looking at the code, if all 3 browsers first accessed the page 2 seconds apart, then all of their requests would be 2 seconds apart (10 seconds in between request intervals though). I think there needs to be a check to delay the refresh the first time to make sure the time (in seconds) is evenly divisible by 10. If not, wait until it is.

 

Hmm, not to be rude, but did you actually read the code? I even provided comments as to what it is doing. And, it does exactly what you state it should do. Upon page load, it calculates the number of millisecond till the next full second (previous script calculated number of millisecond till next 10 second period) and sets the setTimeout() interval to that number of milliseconds.

 

So, if the user loads the page at 1:23:42 and 150 milliseconds, the timer (in the updated script) is set to reload the page at 850 milliseconds (exactly 1:23:43 and 0 milliseconds). The previous script would have set the reload time to 7 seconds and 850 milliseconds (i.e. exact 1:23:50 and 0 milliseconds).

 

So, no matter what time the initial page load occurs, the first sync (and every sync thereafter) will be at the exact same time (based on each PCs system clock).

Edited by Psycho
Link to comment
Share on other sites

Hello and thanks for your interest in helping me out.

 

Let me explain a bit better what i need.

 

i'll have 3 computers.

 

computer number 1 will show a diferent image everyday (365 images/year)

computer number 2 will show a diferent image every minute (1440 images / day)

computer number 3 will show a diferent image every second during one minute (60 images / minute)

 

This needs to be synchonized so when the computer returns to the 0 second is the moment that the computer 2 show the image for the next minute, Does it make sense?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.