Wait until all jQuery Ajax requests are done?

后端 未结 21 1792
离开以前
离开以前 2020-11-21 04:39

How do I make a function wait until all jQuery Ajax requests are done inside another function?

In short, I need to wait for all Ajax requests to be done before I exe

21条回答
  •  你的背包
    2020-11-21 05:23

    I'm using size check when all ajax load completed

    function get_ajax(link, data, callback) {
        $.ajax({
            url: link,
            type: "GET",
            data: data,
            dataType: "json",
            success: function (data, status, jqXHR) {
                callback(jqXHR.status, data)
            },
            error: function (jqXHR, status, err) {
                callback(jqXHR.status, jqXHR);
            },
            complete: function (jqXHR, status) {
            }
        })
    }
    
    function run_list_ajax(callback){
        var size=0;
        var max= 10;
        for (let index = 0; index < max; index++) {
            var link = 'http://api.jquery.com/ajaxStop/';
            var data={i:index}
            get_ajax(link,data,function(status, data){
                console.log(index)
                if(size>max-2){
                    callback('done')
                }
                size++
                
            })
        }
    }
    
    run_list_ajax(function(info){
        console.log(info)
    })

提交回复
热议问题