JQuery AJAX轮询语法

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.

You are surely right, to have created an infinite loop.

But you could just check, if the request has to be made again or not and if not, just process the data (if needed here) and return from the Poll2 function.

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);
        }
    });
}

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.