JQuery AJAX Polling Syntax
ajax, javascript, jquery, polling
Solution
As noted above in my edit I have found a solution in this blog post that roughly accomplishes what I am looking for. As it turns out, I really only want my request to fire once. However, in regards to an "infinite" polling routine, this does the job quite nicely.
(function poll() {
setTimeout(function () {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://somewhere.com/rest/123',
success: function (data) {
MyNamespace.myFunction(data); //DO ANY PROCESS HERE
},
complete: poll
});
}, 5000);
})();
I would note, however, that this routine does not initialize its first poll until 5 seconds after it is executed. Simple piece of code though! Thanks much to the author.
Problem
I'm a bit green, so please bear with me. I need to poll a SharePoint webservice until it returns a value. I believe that I have formatted my code incorrectly. If there is a pre-existing thread that addresses this, please point me to it; my relatively limited understanding may have kept me from recognizing it. ``` function Poll2(){ $.ajax({ //Use an ABSOLUTE reference to your target webservice url: "https://mydomain.com/Sandbox/bitest/_vti_bin/lists.asmx", type: "POST", dataType: "xml", data: soapEnv, success:processResult, complete: Poll2, timeout: 5000, contentType: "text/xml; charset=\"utf-8\"", async: true }); } ``` My desire is that it would poll the service every 5 seconds until success, at which point it would proceed to the "processResult" function where all the data will be handled. I am afraid that I have created an infinite loop by referencing the parent function. -------------------EDIT & NEW CODE------------------- I have found a solution in this blog post that roughly accomplishes what I am looking for. As it turns out, I really only want my request to fire once. However, in regards to an "infinite" polling routine, this does the job quite nicely. ``` (function poll() { setTimeout(function () { $.ajax({ type: 'POST', dataType: 'json', url: 'http://somewhere.com/rest/123', success: function (data) { MyNamespace.myFunction(data); //DO ANY PROCESS HERE }, complete: poll }); }, 5000); })(); ``` I would note, however, that this routine does not initialize its first poll until 5 seconds after it is executed. Simple piece of code though! Thanks much to the author.