Jquery ajax in loop

I am writing a Sql convertor which handles mysql to mongodb. I am writing an interface to my converter with ajax.

Ajax is handling huge convertions. This put limit to mysql select code and make convertions parted by 1000 at a time.

my code is :

$(document).ready(function()
{
<!--Start JQuery Ajax Requests-->
var pages;
var type;
$("input").click(function(event) {
    pages = $(this).attr("icr");
    type = $(this).attr("id");
    runRequest(0);
});

function runRequest(num){

    if (num > 3){
    $("#console").append("Finish!!!");
    return;
    }

    $.ajax
    ({
    type: "POST",
    url: "#",
    async: false,
    data: "type="+type+"&page="+num*1000,
    success: function(msg){
        $("#console").ajaxComplete(function(event, request, settings)
        {
            $("#console").append(msg);
            runRequest(num+1);
        });
    }
    });
}
});

this code have to run 3 times of the same function and send the limit number by multiplying it. But somehow runRequest's num variable never reaches the 3, firebug console shows num as 1 or sometimes 2 and it repeats infinite. How to avoid it and make it run only 3 syncronised calls?

Answers


I believe the problem is your use of .ajaxComplete(). According to the jQuery docs, it is a separate event handler registration method. In other words, your success function is registering an event to fire upon the next completion. I think this would also cause your first data set to be thrown away.

An additional note, you're using the data parameter to pass in data. This is supposed to be an object, not a query string. If you use a GET Ajax call, it gets converted to a query string. url: "?type="+type+"&page="+num*1000, and data: { "type": type, "page": num*1000 } are equivalent.

Try rewriting it this way:

$.ajax({
    type: "POST"
    url: "#",
    async: false,
    data: { "type": type, "page": num*1000 },
    success: function(msg) {
        $("#console").append(msg);
        runRequest(num+1);
    });
}

The .ajaxComplete() method is more for the purpose of inserting a separate function call or behavior into every ajax success handler (when you have many such requests on the same page and want each of them to append to the console, in addition to doing their normal callback). Like this:

$(document).ready(function()
{
    $("#console").ajaxComplete(function(event, request, settings)
    {
        // $(this) is pointing to the #console ID because the function is bound to it
        $(this).append('Triggered ajaxComplete handler. The result is ' +
                 xhr.responseHTML);
        runRequest(num+1);
    });
    // variables

    // document event bindings/handlers

    // Ajax functions (you could move this outside of the .ready function

}

Here's another way:

for(i=0; i<5; i++) {
    $.ajax({
        type: "GET",
        url: '/api/foo/?bar=' + i,
        ajaxI: i,
        success: function(widgets) {
            x = this.ajaxI; // now you can use x instead of i

            // ...
        }
    });
}

Need Your Help

Replicate MySQL vs Replicate static pages

php mysql caching load load-balancing

I am trying to do load balancing between two geo-separated servers and I am thinking that instead of replicating the whole package (i.e., MySQL data/PHP/HTML/Images) to both servers, I would just

Text_area empty

ruby-on-rails

I'm trying to display the content of my message class using text_area helper but when I load the page the text area is always blank.

Setup kerberos delegation automatically

installation kerberos delegation

I have a web app that uses some backend servers (UNC, HTTP and SQL). To get this working I need to configure ServicePrincipalNames for the account running the IIS AppPool and then allow kerberos