Anders
Anders

Reputation: 17564

Can stack overflow with "recursive" callbacks?

I have written a long time polling helper class, since the $.getJSON method i async it will not be recursive and will not overflow right? (Recursive as in responseCallback calls startPolling

MyApp.Polling = function (url, parameters, callback) {
    this.url = url;
    this.parameters = parameters;
    this.callback = callback;
    this.startPolling();
};

MyApp.Polling.prototype = {
    abort: function () {
        if (this.request != null) {
            this.request.abort();
        }
    },
    startPolling: function (xhr) {
        if (xhr != null && xhr.status == 0) return;

        this.request = $.getJSON(this.url, this.parameters, $.proxy(this.responseCallback, this));
        this.request.error($.proxy(this.startPolling, this));
    },
    responseCallback: function (data) {
        this.callback(data);
        this.startPolling();
    }
};

edit: Thanks for quick response guys, I have another question. In firebug there are alot of requests that looks like they are still waiting for response (The little spinner is spinning beside the request) But they should be served (As you see in the code only one request is served at a time), and if I close down the entire webserver they are not aborted they continue to spin, is it a firebug bug or do I have a problem with my long time polling? Thanks

Upvotes: 2

Views: 321

Answers (2)

Matt Fellows
Matt Fellows

Reputation: 6532

True - assumption valid - no recursion there.

Upvotes: 1

Guffa
Guffa

Reputation: 700680

That is correct. You will exit out of the startPolling method and return control to the browser before the success or error events can happen, so the callbacks are not called from within the method.

Upvotes: 2

Related Questions