Ajax polling by using recursion - Memory ramp up? Stack overflows?

B

Brian Takita

Hello, we are implementing a function that uses recursion to poll a
server. This implementation seems like it would cause a memory ramp up
and possibly a stack overflow. Am I correct? Is there anyway to
accomplish polling without this issue?
function poll() {
$.get('/message_queue/' + gaming_session.guid,
function(command_string) {
var messages = JSON.parse(command_string);
for (var i = 0; i < messages.length; i++) {
Model.execute_server_commands(messages);
}
poll();
});
}
poll();
 
D

David Mark

Hello, we are implementing a function that uses recursion to poll a
server. This implementation seems like it would cause a memory ramp up
and possibly a stack overflow. Am I correct? Is there anyway to
accomplish polling without this issue?

You are incorrect. Unless you are using a synchronous request, this
is not recursion. I don't think it makes sense to use an anonymous
function for the callback though. Why not declare the callback
function outside of poll and pass a reference to the get method?
function poll() {
$.get('/message_queue/' + gaming_session.guid,
function(command_string) {
var messages = JSON.parse(command_string);
for (var i = 0; i < messages.length; i++) {
Model.execute_server_commands(messages);
}
poll();
});
}
poll();
 
B

Brian Takita

Hello, we are implementing a function that uses recursion to poll a
server. This implementation seems like it would cause a memory ramp up
and possibly a stack overflow. Am I correct? Is there anyway to
accomplish polling without this issue?

You are incorrect. Unless you are using a synchronous request, this
is not recursion. I don't think it makes sense to use an anonymous
function for the callback though. Why not declare the callback
function outside of poll and pass a reference to the get method?
function poll() {
$.get('/message_queue/' + gaming_session.guid,
function(command_string) {
var messages = JSON.parse(command_string);
for (var i = 0; i < messages.length; i++) {
Model.execute_server_commands(messages);
}
poll();
});
}
poll();


So does the callback invocation create a new stack frame?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top