Alex
Alex

Reputation: 9265

ajax auto refresh stats

I have a script that refreshes a counter in a div every 5 seconds. However on load it doesn't show up until after that 5 seconds, and then continually refreshes at the same interval. How do I make it show for the initial 5 seconds?

<script> 
var auto_refresh = setInterval(
function()
{
$('#refresh-me').load('stats_counter.php?table=testimonials');
}, 5000);
</script>

<!---- stats ---->
<div id="refresh-me">

</div>
<!---- stats ---->

Upvotes: 2

Views: 7570

Answers (4)

Praburaj
Praburaj

Reputation: 11

Ajax counter can be done in easy just include below files

  1. index.html
  2. counter.php (ajax file)
  3. necessary images
  4. JS file (for jquery paging call)

download link: https://docs.google.com/open?id=0B5dn0M5-kgfDcE0tOVBPMkg2bHc

Upvotes: 0

Kylie
Kylie

Reputation: 11749

Create a function., then call it in the setInterval, and once on page load

 function refresh() {
   $('#refresh-me').load('stats_counter.php?table=testimonials');
  }
   var auto_refresh = setInterval(function() { refresh() }, 5000);
   refresh();

Upvotes: 1

Callum Linington
Callum Linington

Reputation: 14417

Call the load method on page load, then start interval!

Upvotes: 0

Reactgular
Reactgular

Reputation: 54801

<script> 
   $(document).ready(function() {
       function count() {
           $('#refresh-me').load('stats_counter.php?table=testimonials');
       }
       var auto_refresh = setInterval(function() { count() }, 5000);
       count();
   });
</script>

Move your code to a function, and call it from both start up and the timer.

EDIT: I can't remember, but you might be able to just use the function for the setInterval.

<script> 
   $(document).ready(function() {
       function count() {
           $('#refresh-me').load('stats_counter.php?table=testimonials');
       }
       var auto_refresh = setInterval(count, 5000);
       count();
   });
</script>

Upvotes: 2

Related Questions