I’m trying to make long poll ajax calls, back to back. The problem with the current way I’m doing it is that I make each successive call from the callback function of the previous call. Is this a problem? Firebug doesn’t show any of my ajax calls as completed, even thought the data is returned and the callback is 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);
}
You’re right, you have to maintain a stack and closures for no purpose when you do long polling that way and depending on the situation and the implementation you might get a stack overflow or at least run low on memory… though I don’t know for sure what optimizations the various js implementations perform (e.g. tail recursion which would make those problems go away).
The easy alternative is to use
window.setTimeout(funcName)which will immediately call the functionfuncNamewhen the current scope resolves, from the global scope.