Chaining multiple ajax calls with jquery deferred objects

后端 未结 1 1570
眼角桃花
眼角桃花 2021-02-10 21:29

While looking for a solution similar to as described here: How to chain ajax calls using jquery I am looking for a solution using jquery v1.52.

I have a set of ajax req

相关标签:
1条回答
  • 2021-02-10 22:00

    Yo! Solved! http://jsfiddle.net/sandhyasriraj/AaHZv/

    var x = null;
    var i = 0;
    x= $.Deferred();
    var countries=["US","CA","MX","bx","fs","ZX"];
    function log(msg) {
        var $out=$("<div />");
        $out.html(msg);
        $("#console").append($out);
    }
    
    
    callX = function(j) {
        return $.ajax({
                type: "GET",
                url: "/echo/json/",
                data: {country:countries[i]},
                dataType: "JSON",
                success: function(){
                    log("Successful request for [" + countries[j] + "]");
                    i++;      
                    x.resolve();
                    xy();
                   }
            });
    
    }
    x.resolve();
    xy = function()
    {
        debugger;
        if(i > 5)
            return;
    
         $.when(x).then(function() {
            x = $.Deferred();
            log("Making request for [" + countries[i] + "]");
            callX(i);
        });
    }
    xy();
    

    is this what you want?

    0 讨论(0)
提交回复
热议问题