Javascript 让循环等待ajax调用

Javascript 让循环等待ajax调用,javascript,Javascript,在进入下一个迭代之前,是否可以让这个循环等待AJAX调用完成 for(curr;curr < total;curr++) { $this.val('Uploading '+(curr+1)+' out of '+total); var tmpStr = JSON.stringify(o[curr]).replace("&", "and"); tmp[0] = JSON.parse(tmpStr); $.ajax({ type: 'P

在进入下一个迭代之前,是否可以让这个循环等待AJAX调用完成

for(curr;curr < total;curr++) {
    $this.val('Uploading '+(curr+1)+' out of '+total);
    var tmpStr = JSON.stringify(o[curr]).replace("&", "and");
    tmp[0] = JSON.parse(tmpStr);

    $.ajax({
        type: 'POST',
        url: 'http://example.com/api/post',
        data: "data="+JSON.stringify(tmp),
        success: function(msg) {

            var d = JSON.parse(msg);
            if (d.success) {

                //localStorage.removeItem("data");
            }
            else
            {
                alert("Data failed to upload, please try again");
            }
        },
        error: function(msg) {
            alert("Data failed to upload, please try again");
            $this.val('Upload to CMS');
        }
    });
}
for(curr;curr
您可以将关键字与递归函数一起使用。
when
关键字允许您等待ajax调用。在ajax调用之后,当函数将结果发送给done函数时,即使它失败了。您可以通过在ajax调用中添加一些错误控制来扩展您的场景。但我举个例子:

function reCursive(curr,total)
{
    if(curr < total) 
    {
        $this.val('Uploading '+(curr+1)+' out of '+total);
        var tmpStr = JSON.stringify(o[curr]).replace("&", "and");
        tmp[0] = JSON.parse(tmpStr);

        $.when(function() 
        {
            return $.ajax(
            {
                type: 'POST',
                url: 'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
                data: "data="+JSON.stringify(tmp),
                success: function(msg) 
                {
                    var d = JSON.parse(msg);
                    if (d.success) 
                    {
                        //localStorage.removeItem("data");
                    }
                    else
                    {
                        alert("Data failed to upload, please try again");
                    }
                },
                error: function(msg) 
                {
                    alert("Data failed to upload, please try again");
                    $this.val('Upload to CMS');
                }
            });
        }).done(function (x) 
        {
            //When you get the result from your ajax call. Write your code here.
            curr++;
            reCursive(curr, total);
        });
    }
}
函数递归(当前,总计)
{
如果(当前<总计)
{
$this.val('上载'+(当前值+1)+'共'+);
var tmpStr=JSON.stringify(o[curr])。替换(“&”,“and”);
tmp[0]=JSON.parse(tmpStr);
$.when(函数()
{
返回$.ajax(
{
键入:“POST”,
网址:'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
数据:“data=“+JSON.stringify(tmp),
成功:功能(msg)
{
var d=JSON.parse(msg);
如果(d.成功)
{
//localStorage.removietem(“数据”);
}
其他的
{
警报(“数据上传失败,请重试”);
}
},
错误:函数(msg)
{
警报(“数据上传失败,请重试”);
$this.val('Upload to CMS');
}
});
}).完成(功能(x)
{
//当您从ajax调用中得到结果时,请在此处编写代码。
curr++;
递归(curr,total);
});
}
}
您可以将关键字与递归函数一起使用。
when
关键字允许您等待ajax调用。在ajax调用之后,当函数将结果发送给done函数时,即使它失败了。您可以通过在ajax调用中添加一些错误控制来扩展您的场景。但我举个例子:

function reCursive(curr,total)
{
    if(curr < total) 
    {
        $this.val('Uploading '+(curr+1)+' out of '+total);
        var tmpStr = JSON.stringify(o[curr]).replace("&", "and");
        tmp[0] = JSON.parse(tmpStr);

        $.when(function() 
        {
            return $.ajax(
            {
                type: 'POST',
                url: 'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
                data: "data="+JSON.stringify(tmp),
                success: function(msg) 
                {
                    var d = JSON.parse(msg);
                    if (d.success) 
                    {
                        //localStorage.removeItem("data");
                    }
                    else
                    {
                        alert("Data failed to upload, please try again");
                    }
                },
                error: function(msg) 
                {
                    alert("Data failed to upload, please try again");
                    $this.val('Upload to CMS');
                }
            });
        }).done(function (x) 
        {
            //When you get the result from your ajax call. Write your code here.
            curr++;
            reCursive(curr, total);
        });
    }
}
函数递归(当前,总计)
{
如果(当前<总计)
{
$this.val('上载'+(当前值+1)+'共'+);
var tmpStr=JSON.stringify(o[curr])。替换(“&”,“and”);
tmp[0]=JSON.parse(tmpStr);
$.when(函数()
{
返回$.ajax(
{
键入:“POST”,
网址:'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
数据:“data=“+JSON.stringify(tmp),
成功:功能(msg)
{
var d=JSON.parse(msg);
如果(d.成功)
{
//localStorage.removietem(“数据”);
}
其他的
{
警报(“数据上传失败,请重试”);
}
},
错误:函数(msg)
{
警报(“数据上传失败,请重试”);
$this.val('Upload to CMS');
}
});
}).完成(功能(x)
{
//当您从ajax调用中得到结果时,请在此处编写代码。
curr++;
递归(curr,total);
});
}
}

虽然不能使
的循环等待,但可以重写代码以使用另一种调用方式运行

function runOnce(curr) {
  $this.val('Uploading ' + (curr + 1) + ' out of ' + total);
  var tmpStr = JSON.stringify(o[curr]).replace("&", "and");
  tmp[0] = JSON.parse(tmpStr);

  $.ajax({
    type: 'POST',
    url: 'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
    data: "data=" + JSON.stringify(tmp),

    // ***ADDED HERE*
    always: {
      if (curr < total) {
        runOnce(curr+1);
      } else {
        alert("All done-- you could call your next function from here.");
      }
    },
    success: function (msg) {

      var d = JSON.parse(msg);
      if (d.success) {
        //localStorage.removeItem("data");
      } else {
        alert("Data failed to upload, please try again");
      }
    },
    error: function (msg) {
      alert("Data failed to upload, please try again");
      $this.val('Upload to CMS');
    }
  });
}

// Set things up and run it once.
// The '0' is what the loop index would be.
runOnce(0);
功能跳动(当前){
$this.val('上载'+(当前值+1)+'共'+);
var tmpStr=JSON.stringify(o[curr])。替换(“&”,“and”);
tmp[0]=JSON.parse(tmpStr);
$.ajax({
键入:“POST”,
网址:'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
数据:“data=“+JSON.stringify(tmp),
//***此处添加*
始终:{
如果(当前<总计){
跳动(电流+1);
}否则{
警报(“全部完成——您可以从这里调用下一个函数。”);
}
},
成功:功能(msg){
var d=JSON.parse(msg);
如果(d.成功){
//localStorage.removietem(“数据”);
}否则{
警报(“数据上传失败,请重试”);
}
},
错误:函数(msg){
警报(“数据上传失败,请重试”);
$this.val('Upload to CMS');
}
});
}
//设置并运行一次。
//“0”是循环索引的内容。
跳动(0);

虽然不能使的循环等待,但可以重写代码以使用另一种调用方式运行

function runOnce(curr) {
  $this.val('Uploading ' + (curr + 1) + ' out of ' + total);
  var tmpStr = JSON.stringify(o[curr]).replace("&", "and");
  tmp[0] = JSON.parse(tmpStr);

  $.ajax({
    type: 'POST',
    url: 'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
    data: "data=" + JSON.stringify(tmp),

    // ***ADDED HERE*
    always: {
      if (curr < total) {
        runOnce(curr+1);
      } else {
        alert("All done-- you could call your next function from here.");
      }
    },
    success: function (msg) {

      var d = JSON.parse(msg);
      if (d.success) {
        //localStorage.removeItem("data");
      } else {
        alert("Data failed to upload, please try again");
      }
    },
    error: function (msg) {
      alert("Data failed to upload, please try again");
      $this.val('Upload to CMS');
    }
  });
}

// Set things up and run it once.
// The '0' is what the loop index would be.
runOnce(0);
功能跳动(当前){
$this.val('上载'+(当前值+1)+'共'+);
var tmpStr=JSON.stringify(o[curr])。替换(&“,”);
tmp[0]=JSON.parse(tmpStr);
$.ajax({
键入:“POST”,
网址:'http://mvc.sivapi.sheffieldnetwork.com/form/submit/submit/meadowhallEaster/',
数据:“data=“+JSON.stringify(tmp),
//***此处添加*
始终:{
如果(当前<总计){
跳动(电流+1);
}否则{
警报(“全部完成——您可以从这里调用下一个函数。”);
}
},
成功:功能(msg){
var d=JSON.parse(msg);
如果(d.s