Webrsk
Webrsk

Reputation: 1026

Refresh a PHP page for every predefined seconds

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.

Upvotes: 0

Views: 7323

Answers (6)

nonopolarity
nonopolarity

Reputation: 151006

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.

Upvotes: 2

nonopolarity
nonopolarity

Reputation: 151006

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.

Upvotes: 1

Eduardo Scoz
Eduardo Scoz

Reputation: 24753

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.

Upvotes: 1

pts
pts

Reputation: 87271

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.

Upvotes: 4

Yuval Adam
Yuval Adam

Reputation: 165242

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.

Upvotes: 2

ozke
ozke

Reputation: 1620

With jQuery you can do it):

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

:)

Upvotes: 0

Related Questions