views:

980

answers:

7

I would like to load/refresh a particular page for every 10 secs to view updated data's fetched from Database.

I used META for doing it

 <META HTTP-EQUIV=Refresh CONTENT='10; URL=livedata.php'>

But still i agree we also do this by using :

  • Javascript to load a div by settimeout
  • Ajax dynamic refresh

Would be great if you share the performance issues using META , AJAX dynamic refreshing , Javascript settimeout .. Also share the best way of doing it.

Note : Need to refresh whole page rather than specific frame or div.

A: 

With jQuery you can do it):

var seconds = 10;
var id = setInterval(function()
{
     $('#container').load('whatever.php');
}, 1000*seconds);

:)

ozke
It is no doubt we can do it with jQuery, and even without it, using pure JavaScript, or just a META tag. But which one is better and why?
pts
It´s up to you really. The reason to use jQuery is because it´s simple and easy. You just saw the 4/5 lines of code. The only reason not to use it is you need to load the few kb of jQuery but it´s quick to load, everybody is using it and the size of the minified/zipped version is tiny.
ozke
+2  A: 

If you plan on refreshing the entire page, using <META> tags is the cleanest way. It just seems awkward to have a JS timer refreshing your page when you have a fully-supported HTML-only way of doing this.

However, if you just need a specific part of the page refreshed, use AJAX. It's better in terms of user experience, as well as performance.

Yuval A
+3  A: 

Using AJAX is the least intruisive to the user, because the user doesn't notice that something is being refreshed/reloaded until it is complete.

Please note that AJAX can perform better or worse than META depending on the situation:

  • If the data to be updated is small with respect to the full HTML page size, AJAX is better than META, because with AJAX you can send only the data difference, and/or you can send data in more compact format than HTML.
  • Running JavaScript puts a burden to the user's browser. If the user has 20 tabs open (which is not uncommon), and each of them runs some setTimeout in the background, it can make a huge difference in browser respoinsiveness to convert all of them to JavaScript-free refresh.
pts
I agree with your points. I would like to refresh the complete page rather than specific frame or div as there are dynamic variables need to pass to it.Is META has any bottleneck on performance aspect?
Webrsk
A: 

You might find this question useful:

http://stackoverflow.com/questions/719191/ajax-refresh-script/719198#719198

karim79
A: 

For the client, there's really no difference between all the methods you mentioned. The only difference I can find is that using doesn't require javascript, like other solutions do, but nowadays everybody has javascript anyway.

The big difference, to me, is in the server usage. If you have 100 users refreshing every 10 seconds, that's already about 10 reqs/sec. Depending on the logic you have to generate the page (which is likely dinamic), this may cause the server usage to skyrocket. Make sure you're careful about that.

Eduardo Scoz
A: 

note that you can also use header() in PHP to accomplish what the meta tag is doing too. Just make sure you make the header() call before other output.

動靜能量
A: 

using javascript to fetch dynamic content has one more benefit: if the content doesn't load for one time, it can still keep trying. if you reload the whole page and it doesn't load, then it would stop there.

also if you use Ajax, then the display is nicer because you don't see the whole page blanking out and re-rendering again and again.

動靜能量