如何延迟 3 个 ajax 调用之一?

How to delay one of the 3 ajax calls?

我需要这些 AJAX 表格的帮助。我需要在其他人之后执行最小的一个,我想延迟它。

我尝试使用 setTimeout(),但它似乎不适合我。

我需要它,因为较小的 AJAX 和 overalleff.php 正在根据其他两个 AJAX 请求进行计算。

// THIS IS THE AJAX FOR THAT I WANT TO DELAY
// THIS AJAX NEED TO BE EXECUTED AFTER OTHERS
function pbutton() {
  $.ajax({
    url: "overalleff.php",
    success: function(data) {
      setTimeout(function() {
        $data
      }, 2000);
    }
  });
}

var barray = [];

function cbutton() {
  $('input:radio[name="cheking"]:checked').val();
  var varies = $("#fromdate").val();
  var varies2 = $("#todate").val();
  if (varies == "" || varies2 == "") {
    alert("Please fill in all 2 fields first and then submit again");
  } else {
    barray.push(varies + "~" + varies2);
    $.ajax({
      type: "POST",
      url: "pickingeff.php",
      data: {
        fromdate: varies,
        todate: varies2
      }
    }).done(function(msg) {});
  }
}

var sarray = [];

function abutton() {
  var varies3 = $("#fromdate").val();
  var varies4 = $("#todate").val();
  if (varies3 == "" || varies4 == "") {
    alert("Please fill in all 2 fields first and then submit again");
  } else {
    sarray.push(varies3 + "~" + varies4);
    $('#loadingmessage').show();
    $.ajax({
      type: "POST",
      url: "packingeff.php",
      data: {
        fromdate: varies3,
        todate: varies4
      }
    }).done(function(msg) {
      $('#loadingmessage').hide();
    });
  }
}

由于 $.ajax returns a Promise,您可以使用 Promise.all 在所有其他承诺结束后运行函数。

function makeThreeAjaxCalls() {
    let p1 = $.ajax("url1");
    let p2 = $.ajax("url2");
    p1.then(callback1)
    p2.then(callback2)
    Promise.all([p1,p2]).then(function() {
        $.ajax("url3").then(callback3);
    });
}