Reputation: 9265
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
Reputation: 11
Ajax counter can be done in easy just include below files
download link: https://docs.google.com/open?id=0B5dn0M5-kgfDcE0tOVBPMkg2bHc
Upvotes: 0
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
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