Page 1 of 1

JQuery Loop -- "Too Much Recursion" Error

Posted: Mon Jun 01, 2009 2:39 pm
by joshmaker
I am currently using jQuery to pull a random quote from a database via AJAX and display it at the top of my website.

Code: Select all

    function refreshQuote() {
        setTimeout("refreshQuote()", 7000);
        $("#Quote").hide("slow", function() {
        $(this).load("/quote/random/").ajaxComplete(function() {
            $(this).show("slow");
        });
        }
    }
The trouble is, if I allow the website to sit for a while, it eventually starts throwing off "Too Much Recursion" errors and the web browser bogs down.

I assume that there is a proper way to set up a nice loop in Javascript without memory leaks, but I'm at a loss. Can anyone offer a suggestion?

Re: JQuery Loop -- "Too Much Recursion" Error

Posted: Mon Jun 01, 2009 3:23 pm
by kaszu
I don't see why there could be too much recursion, since this function is not recursive :/
Only problem what I see is if requests are taking a lot of time, then next refreshQuote will start before previous has 'ended'.
Try following (removed anonymous functions and moved setTimeout to execute after ajax request):

Code: Select all

 
    function showQuote () {
         $("#Quote").show("slow");
        setTimeout(refreshQuote, 7000);    //Call after ajax has completed
    }
    function loadNextQuote () {
         $("#Quote").load("/quote/random/").ajaxComplete(showQuote);
    }
    function refreshQuote() {
        $("#Quote").hide("slow", loadNextQuote);
    }
Btw, $("#Quote").hide was not closed (maybe this was creating the problem?).