简体   繁体   中英

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);

});

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

My guess is there is an exception when tv.php is being loaded. Check your console output for possible exceptions. You could also try adding a try/catch:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM