7

I want to call a function within itself like this:

$(document).ready ( 

    function ready() {
        var tester = $.ajax({
                async: false,
                url: "test_parse.php"
            }).responseText;
        document.getElementById('test').innerHTML = tester;
        setTimeout(ready(), 3000); 
   }
    
);

But every time I do this my browser just keeps loading and eventually Apache shuts down (obviously not my expected result). Could you help me to figure out a solution?

4
  • Can you call setTimeout from outside the function? Commented Sep 10, 2011 at 4:48
  • also I don't think it's correct to put functions inside $(document).ready({}); Commented Sep 10, 2011 at 4:50
  • 1
    @Devin - document.ready expects a function expression as a parameter; it doesn't matter whether said function has a name. (Before anyone points it out, I know that a function expression is not the only option, but I think it is the most commonly used option.) Commented Sep 10, 2011 at 5:26
  • 1
    Keep in mind that named function expressions have many quirks and issues associated with them (including browser bugs and even memory leaks), so I'd avoid using them in production code. Commented Sep 10, 2011 at 5:59

4 Answers 4

11

setTimeout takes a function reference:

setTimeout(ready, 3000); 

not

setTimeout(ready(), 3000); 

And that being said, I would also do this:

$(document).ready ( 

    function ready() {
        var tester = $.ajax({
                url: "test_parse.php",
                success: function (data) {
                    document.getElementById('test').innerHTML = data;
                    setTimeout(ready, 3000); 
                }
            })
   }

);

Because async: false will lock up the browser until that data returns from the server

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

4 Comments

maybe he put async:false there for a legitimate reason?
It seems that he wants to make a synchronous request... do you have a problem with this?
@Travis: There is no good reason to make a synchronous XHR request. 8.4% of reported IE9 hangs last month were due to synchronous XHR. It's bad user experience; there's no reason that what the OP is trying to do can't be accomplished in a success callback function.
Why would you put function inside $(document).ready() ? Why would you use $(document).ready() instead of $(function) {}; ?
7

This is wrong:

setTimeout(ready(), 3000); 

This is right:

setTimeout(ready, 3000); 

ready() is actually invoking the function. ready is simply a reference to the function, which is what you want.

1 Comment

Awesome! Thanks. I knew it was something small.
2

setTimeout expects a function reference as the first parameter, you have a function invocation which is passing the result of calling ready().

This is causing an infinite loop.

You need to pass in "ready", not "ready()"

setTimeout(ready, 3000);

And if you're trying to queue ajax requests that happen in a structured order, you'll want to fire the setTimeout on success after the previous ajax call, not immediately, otherwise you'll have ajax results returning and updating at arbitrary intervals depending on how the server responds to each request.

$.ajax({
    // your ajax settings
}).success(function () {
    // fire off the next one 3 secs after the last one completes
    setTimeout(ready, 3000);
});

This is better than using the async: false setting, which blocks the browser.

Comments

0

Why are you trying to call the function within itself? Surely all you need to do is move setTimeout outside of the function, then call ready() every 3000ms? What'l do you want your output to be?

1 Comment

ready() calls itself (via the success callback) so that things won't get backed if one of the $.ajax calls takes too long. I tend to use this approach instead of setInterval precisely to keep things nicely in sequence and not backed up.

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.