Refresh a PHP page for every predefined seconds

后端 未结 6 1037
一个人的身影
一个人的身影 2021-01-26 17:06

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

 

        
相关标签:
6条回答
  • 2021-01-26 17:30

    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.

    0 讨论(0)
  • 2021-01-26 17:31

    With jQuery you can do it):

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

    :)

    0 讨论(0)
  • 2021-01-26 17:36

    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.
    0 讨论(0)
  • 2021-01-26 17:36

    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.

    0 讨论(0)
  • 2021-01-26 17:40

    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.

    0 讨论(0)
  • 2021-01-26 17:45

    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.

    0 讨论(0)
提交回复
热议问题