Ajax re-polling without recursive callback function

I am trying to do a long poll of ajax calls, back to back. The problem with the current way I am doing is that I make each subsequent call from the callback function of the previous call. This is problem? Firebug doesn't show any of my ajax calls as complete, even thought the data was returned and the callback was being executed. The recursive structure seems inefficient. Any ideas?

window.addEvent('domready', function()
{
    server =  new Request({
    url: "chat.php",
        method: 'get',
        link:   'ignore',
        onSuccess: callback,
    });

    request = server.send();
}

function callback(data)
{
    console.log(data);
    var data = JSON.decode(data);
    messId = data.max;
    for(var i = 0; i < data.messages.length; i++)
    {
        print("", data.messages[i].text);
    }
    var sendString = "messId="+messId;
    request = server.send(sendString);
}

      

+2


source to share


1 answer


You are right, you need to maintain stacks and closures for the target when you do long polling this way, and depending on the situation and implementation, you might get a stack overflow, or at least run into memory ... although I don't know for sure. what optimizers do different js implementations (like tail recursion which will make these problems go away).



An easy alternative is to use window.setTimeout(funcName)

that will immediately call the function funcName

when the current scope is removed from the global scope.

+1


source







All Articles