Reputation: 63
I'm having a problem with how can I stop automatically the auto_refresh of my script. Here's my code:
<script type="text/javascript">
function refresh_question(){
var url = "/question.do";
jQuery('#question').load(url, function myFunction(reponse, status, xhr){
if(reponse=="fin")
{
clearInterval(auto_refresh );
}
}).fadeIn("slow");
}
var auto_refresh = setInterval(refresh_question, 4000);
</script>
The idea behind this script is that I want to display a new question of my DB each 4 seconds, but after the 10th question I should stop the quiz, means turn off the auto_refresh. How can I do so?
Upvotes: 1
Views: 1726
Reputation: 16636
Just use a counter that is incremented each time the refresh been called:
<script type="text/javascript">
var numberOfRefreshes = 0;
var auto_refresh = 0;
function refresh_question(){
var url = "/question.do";
jQuery('#question').load(url, function myFunction(reponse, status, xhr){
numberOfRefreshes++;
if(numberOfRefreshes == 10)
{
clearInterval(auto_refresh);
}
}).fadeIn("slow");
}
auto_refresh = setInterval(refresh_question, 4000);
</script>
Upvotes: 1