Dean Dean - 3 months ago 6
Javascript Question

How to get jQuery infinitely repeat function?

I currently have the following piece of code. When "true" is returned from tv.php the code seems to stop. How can I make this code check tv.php every 60 seconds, and repeat regardless of what TV.php returns? (I want the to refresh every time it detects "true", and keep checking every 60 seconds infinitely)

$(document).ready(function(){
setInterval(function() {
$.ajax({
url: 'checktime.php',
success: function(refresh){
if($.trim(refresh) == "true"){
$('#tv').load('tv.php');
}
}
});
}, 60000);

});

Answer

Try with:

    function ref(){
        $.ajax({
            url: 'checktime.php',
            success: function(refresh){
                if($.trim(refresh) == "true"){
                    $('#tv').load('tv.php', function(){
                         setTimeout(ref, 60000); // Do again after 60s
                    });
                }   
            }
        });
    }

    ref(); // First run