jquery - 如何同步两个嵌套的jquery foreach循环?

标签 jquery ajax

在内循环的最后一次迭代中,每个循环有两个嵌套,我想调用一个包含 ajax 调用的 jquery 函数。只有从这个 ajax 调用获得结果后,才会发生每个外部的下一次迭代。有什么办法可以同步这些循环。

$.ajax({
        type: "POST",
        contentType: 'application/json;charset=utf-8',
        dataType:'json',
        url: 'getCall1',
        data: JSON.stringify(send_data),
        success: function(json)
        {
          $.each(json,function(i,item){
            $.ajax({
                    type: "POST",
                    contentType: 'application/json;charset=utf-8',
                    dataType:'json',
                    url: 'getcall2',
                    data: JSON.stringify(send_data),
                    success: function(json)
                    {
                      $.each(json,function(i,item){
                       //For last iteration of this loop i want to call a function contain another ajax call. After completion of this call i want to continue with next iteration of outer loop
                        callFun();
                      });
                    }
             });
      });
   }

});

function callFun(){
  $.ajax({
        type: "POST",
        contentType: 'application/json;charset=utf-8',
        dataType:'json',
        url: 'getcall3',
        data: JSON.stringify(send_data),
        success: function(json)
        {

        }
  });
}

最佳答案

您可以使用回调函数来处理它:

$.ajax({
    type: "POST",
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    url: "getCall1",
    data: JSON.stringify(send_data),
    success: function(json)
    {
        var keys = Object.keys(json);
        if(keys.length)
        {
            func1(0, keys, json);
        }
    }

});

function func1(index, keys, json1)
{
    //you can get values from json1 using:
    var val = json1[keys[index]];
    //and use val
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        url: "getcall2",
        data: JSON.stringify(send_data),
        success: function(json2)
        {
            var keys2 = Object.keys(json2),
                i;
            if(keys2.length)
            {
                for(i = 0; i < keys2.length; i++)
                {
                    //do as you wish
                    if(i === (keys2.length - 1))
                    {
                        callFun(
                            function callBack()
                            {
                                if(index < keys.length)
                                {
                                    func1(++index, keys, json1);
                                }
                            }
                        );
                    }
                }
            }
        }
    });
}

function callFun(callbackFunc)
{
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        url: "getcall3",
        data: JSON.stringify(send_data),
        success: function(json)
        {
            callbackFunc();
        }
    });
}

祝你好运

关于jquery - 如何同步两个嵌套的jquery foreach循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266147/

相关文章:

javascript - jQuery 在复选框更改时设置下拉值

c# - 加载 View 状态失败。只是偶尔发生。难以重现

javascript - 使用 Firefox 访问 Restdb.io API 时收到错误

JSONP 的 jQuery.ajax() 返回 200,但解析错误运行错误回调而不是成功回调

javascript - 如何在 if 语句中获取另一个 id 下的 id?

javascript - 如何自动化 JavaScript 函数来自动播放幻灯片?

javascript - 将动态网页存储为对象的最佳方式是什么?

jQuery.getScript : data variable in callback undefined

javascript - 我无法通过 ajax 从 View 到 Controller 获取 id。但 id 显示在警告框中

javascript - $.getJSON 竞争条件中的 $.each?