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?

有帮助吗?

解决方案

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top