SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery and setinterval

    i have a number let say ... number of visitors for a page (it's not that but similar in some way)

    i'm using jquery to show how many visitors the site got , and i have a div that contain the number let say

    <div id="counter"> </div>

    and in the jquery i have the followings :

    Code:
    <script>
    
    // the call for the update (it just change an entry in the db total = total + 1;	
    	function autoupdate() {
    	
    	$.post("count.php", {count:1},
      function(data){
       $("#counter").html(data);
      });}
    
    // the 1 sec interval  
      $(document).ready(function() {
        setInterval("autoupdate()", 1000);
      }); 
      </script>

    question : would that be too harsh on a shared server if it receive a lot of visitors ? any better solution (other than making it 5 sec's or more ..)

    my aim is to show a live counter , with live update like g-mail (like how many mb they are hosting .. i know their example is fake ...but wanted to do something similar)
    Last edited by commandos; Dec 31, 2008 at 01:03.
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?

  2. #2
    SitePoint Enthusiast stakes's Avatar
    Join Date
    Sep 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Obviously it depends on what your definition of "many visitors" are. I don't know how your back-end php script works but I'm guessing it's a pretty resource cheap operation? Altough it will generate alot of http requests, I think you're relativetly safe with it.
    webcloud.se - Articles on web development

  3. #3
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use an Ajax request with the ifModified header.
    Or you could just use the load function.
    The one second interval creates a huge amount of requests, which I wouldn't recommend...
    Nobody is really looking to the counter every second, so you can increase the interval without upsetting anybody.

    What really concerns me is what happens if counter.php could not be loaded within the one second interval.
    Imagine someones internet connection breaks down right after they visit your website.
    The page counter.php could not be loaded, and after 30 seconds you'll have 30 open connections...
    So don't use setInterval with ajax requests, but rather use setTimeout after the ajax request completed or returned an error.

    I would recommend something like below, but you could use one of the other ajax functions of jQuery, whichever suits you best.

    Code JavaScript:
    jQuery(document).ready(loadCounter);
     
    function loadCounter () {
        $("#counter").load("counter.php", {}, function(responseText, textStatus, XMLHttpRequest){
            setTimeout(loadCounter, 5000);
        });
    }
    FOR SALE: 1 set of morals, never used, will sell cheap

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Hexburner , i appreciate it !
    Thai Boxing
    E-Pix
    Chaaban
    Tekfirm Want to have a web site ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •