2

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 ---->
4

4 回答 4

2
<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>
于 2013-06-23T23:15:51.190 回答
1

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();
于 2013-06-23T23:16:57.870 回答
0

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

于 2013-06-23T23:16:33.593 回答
0

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

于 2015-01-30T10:47:55.183 回答