Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery AJAX Polling Syntax

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.

like image 881
Shrout1 Avatar asked Dec 05 '25 10:12

Shrout1


2 Answers

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.

like image 130
Shrout1 Avatar answered Dec 08 '25 00:12

Shrout1


The timeout setting tells jQuery how long it should wait for a response from the server before giving up.

function Poll2(){
    $.ajax({
        //Use an ABSOLUTE reference to your target webservice
        url: "https://example.com/Sandbox/bitest/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        success:processResult,
        timeout: 5000,
        contentType: "text/xml; charset=\"utf-8\"",
        async: true
    });
}

If you want it to keep trying every five seconds until success, you could do something like this:

function Poll2(){
    $.ajax({
        //Use an ABSOLUTE reference to your target webservice
        url: "https://example.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
        error: function(xhr) {
            setTimeout(Poll2, 5000);
        }
    });
}
like image 37
Vivian River Avatar answered Dec 07 '25 22:12

Vivian River



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!