-1

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?

7
  • 2
    Are you sure that your AJAX call is working, and that it is accessing the page you want? Remember AJAX is * asynchronous* so it doesn't matter if you call it every 10 seconds, it may take longer than that to find a response. Commented Mar 5, 2013 at 10:58
  • And are you sure the data change and the file isn't cached ? Commented Mar 5, 2013 at 10:59
  • The whole javascript file is evaluated before anything is run, @ppeterka, it's not a problem with positioning :) Commented Mar 5, 2013 at 11:00
  • Add breakpoints, debug, so many things could have been wrong here but only you can tell if the success function is called. Commented Mar 5, 2013 at 11:00
  • @niaccurshi that's why I didn't pu tthat as an answer.. If I was a JS engine - I'd be broken it turns out :) Commented Mar 5, 2013 at 11:01

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Placing the setTimeout function inside "success" did the trick. Thanx :)
1

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

Comments

1

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>

8 Comments

This won't change anything.
@user1737909 i know there are more errors in OP code but main concern was about not looping periodically so i cleared that first and BTW you can test it function test() {} var refreshId = setInterval(test, 5000); this will work too...so down-vote for that is really silly..
This is one of the things working fine in the original code. I agree it's the same (and it's easier) but that's not an actual solution.
@user1737909 if u see there are $(document).ready(function() { and other jQuery initialization code too...
@arjun if you want to set when its first return success response then its fine but it will not be added if you want to setTimeout no matter if first ajax call fail for some reason..
|
0

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.

1 Comment

What is the Problem below? This doesn't look like a real answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.