views:

32

answers:

1

Hello. I have a website that has members. I want to create a feature that is a real time (with out page load ) counter of how many members are signed in. I have the signed in user in a variable so I'm looking for someway to incorporate it using that. Anyone know of a good plug-in or Jquery script to do this?

thanks in advance

+7  A: 

You really don't need a plugin so much, just create a page that does nothing but echo the count then call that page on an interval, for example:

function updateCount() {
  $('#userCount').load('myCounter.php', function() {
     setTimeout(updateCount, 2000);
  });
}
$(updateCount); //run on document.ready

This uses .load() to fetch the count and place it in the #userCount element (a <span> or <div>, whatever it is), wait 2 seconds and do it again. You want to avoid setInterval() in these cases because the ajax request time is an unknown, and you don't want those to start overlapping. Instead, use setTimeout() so it runs again 2 seconds (or however often you want) after the request completes.

Nick Craver