简体   繁体   中英

Javascript: setInterval function not working

I have the following piece of code. Interval is set to 10sec.

(function($){ 
    $(document).ready(function() { 
        disp_log(); 
        setInterval(function () {
            disp_log();
        }, 10 * 1000);

        function disp_log() { 
            $.ajax({ 
                "type" : "GET", 
                "url" : "/get_log/", 
                "dataType" : "json", 
                "cache" : false, 
                "success" : function(json) { 
                    data=""
                    for(var j = 0; j < json.length; j++) { 
                        data+="<tr><td>"+json[j]+"</td></tr>"
                    } 
                    $("#log").html(data);
                } 
            })(jQuery); 
        }
    }); 
})(django.jQuery); 

But refreshing dosen't happen. Can someone plz tell why?

Use it as below.

setInterval(disp_log, 10 * 1000);

if you function is in global then use it as

setInterval("disp_log()", 10 * 1000);

also you don't need (jQuery) after end of ajax call.

And more important you have (function($){ and $(document).ready(function() { . You don't need it because both are same use either one.

Working Tested Code

<script type="text/javascript">

    $(document).ready(function()
    {
        disp_log(); 
        setInterval(disp_log, 10 * 1000);

        function disp_log()
        {
            $.ajax({ 
                "type" : "GET", 
                "url" : "/get_log/", 
                "dataType" : "json", 
                "cache" : false, 
                "success" : function(json)
                { 
                    var data;
                    for(var j = 0; j < json.length; j++)
                    { 
                        data+="<tr><td>"+json[j]+"</td></tr>"
                    } 
                    $("#log").html(data);
                } 
            }); 
        }
    }); 

</script>

You have an error here : $.ajax does not return you something to call jQuery on. No need for that (jQuery)

The thing you need to do here is debug. First of all work out if you are getting any code back at all. You can do this by using....

"success" : function(json) { 
    console.log(json);
    //or..
    alert(json);
} 

If they don't return anything, then your AJAX request is the problem, not your setInterval code.

However on your setInterval code, you should know that the ajax call could take any time to load, so you shouldn't just keep running it. A better way would be to use setTimeout....

setTimeout(disp_log,10*1000);

Then, inside your success function, put that same code in again...

"success" : function(json) { 
    setTimeout(function() {
        disp_log()
    },10*1000);
} 

This will ensure that your code keeps running 10 seconds after the last time the data was successful. There are other issues to consider (how you keep the script running if the ajax call fails, for example) but this will ensure you don't end up getting out of sync with your server requests!

PROBLEM MAY BE OF THE BROWSER

I had a problem using this setInterval() function, but later I found it was the problem of my mozilla. When I tried to run in Chrome, it was perfect.

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