Looking for a good JQuery counter [closed]
We don’t allow questions seeking recommendations for books开发者_JS百科, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionI 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
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.
Full code
<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
$hits[0];
?>
<script type="text/javascript">
var intcnt = <?php echo $hits[0];?>;
//document.write(intcnt);
var myCounter = new flipCounter('fct', {value:intcnt, inc:123, pace:600, auto:false});
</script>
精彩评论