javascript - Efficient way to long poll in jquery -


i beginner in javascript , jquery , looking efficient way long poll in jquery. came across piece of code , puzzled attribute complete: . aware of use of 'complete' piece of code result in recursive function call?

(function poll(){     $.ajax({ url: "server", success: function(data){         //update dashboard gauge         salesgauge.setvalue(data.value);      }, datatype: "json", complete: poll, timeout: 30000 }); })(); 

thanks in advance. :)

you can avoid recursion using short timeout ensure current call stack clears:

(function poll(){     $.ajax({         url: "server",         success: function(data){             //update dashboard gauge             salesgauge.setvalue(data.value);         },         datatype: "json",         complete: function () {             settimeout(poll, 0);         },         timeout: 30000     }); })(); 

this execute poll (probably) next event after current execution clears. in other words, jquery execute complete callback , return until it's out of onreadystatechange callback, @ point javascript runtime execute poll again (unless different event came in , got onto event queue first, of course).


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -