如何在循环中的函数中使用jQuery?

时间:2016-03-09 23:18:53

标签: javascript jquery ajax

我有一个循环遍历表中N行的函数。

packet.Extract()

这会调用一个名为submitRowToBeProcessed的函数,该函数为每一行执行Ajax调用。

function runStuff() {
    var databasename = $("#MainContent_ddlTestCaseDB").val();
    var action = $("#MainContent_DropDownList1").val();
    var aPos;
    var aData;
    var caseName;
    var configId;
    var partNumber;
    var runCommand;

    swal({
        title: 'Processing ...',
        showConfirmButton: false,
        showLoaderOnConfrirm: true
    });

    if (action === "ConfigureSelected" || action === "UpdateSelecedCases") {
        $('tr:has(input:checkbox:checked)').each(function () {
            aPos = oTable.row( this ).index();
            aData = oTable.row(aPos).data();

            caseName = aData["CaseName"];
            configId = aData["ConfigId"];
            partNumber = aData["PartNumber"];
            runCommand = action === "ConfigureSelected" ? "B" : "UTC";

            submitRowToBeProcessed("MassReRun", databasename, caseName, configId, partNumber, runCommand);
        });
    }
    else if (action === "RunAll" || action === "UpdateCasesRunAll") {
        $('#tblCases tbody tr').each(function () {

            aPos = oTable.row(this).index();
            aData = oTable.row(aPos).data();

            caseName = aData["CaseName"];
            configId = aData["ConfigId"];
            partNumber = aData["PartNumber"];
            runCommand = action === "RunAll" ? "B" : "UTC";

            submitRowToBeProcessed("MassReRun", databasename, caseName, configId, partNumber, runCommand);

        });
    }

    location.reload();
}

我知道我应该以某种方式使用function submitRowToBeProcessed(action, databasename, caseName, configId, partNumber, runCommand) { //$("#loading-indicator-box").show(); $.ajax({ type: 'POST', url: 'process.aspx', data: { command: action, DatabaseName: databasename, CaseName: caseName, ConfigId: configId, PartNumber: partNumber, RunCommand: runCommand }, //async: false, beforeSend: function (xhr) { }, success: function() { //$("#loading-indicator-box").hide(); }, error: function() { //$("#loading-indicator-box").hide(); } }); } 函数来等待所有这些ajax请求完成,但是当ajax请求的数量是动态的时候,我不知道该怎么做。

最终我想向最终用户显示处理消息,直到所有的ajax请求都回来。如果我可以获得该消息来更新行计数处理2或行计数处理的进度1,那就更好了。

1 个答案:

答案 0 :(得分:1)

您的代码是如何构建的并不完全清楚,但可能您可以将所有AJAX请求存储在一个数组中,然后使用apply()函数上的$.when()来确定何时所有请求已完成。

您要做的第一件事就是从SELECT Customers.cust_id, Customers.cust_name, COUNT(*) ords FROM Orders, Customers WHERE Orders.cust_id = Customers.cust_id GROUP BY 1,2 HAVING COUNT(*)<2 函数返回您的AJAX Promise

submitRowToBeProcessed()

然后在你的循环之前定义一个存储你的请求的数组,以及一个正在进行多少请求的计数器。

function submitRowToBeProcessed() {
  return $.ajax({...}); //fill your own settings
}

在循环中,您将向该行添加一个类,该类将指示给用户。您将存储AJAX承诺以两种不同的方式使用:一次完成一个请求以删除&#34;处理&#34; class,并且一次添加到未完成请求的集合中。

var requests = [];
var inProgressCount = 0;

现在,在循环结束时执行以下操作:

$('tr:has(input:checkbox:checked)').each(function () {
  //store a reference
  var row = $(this);

  //show row as being processed
  row.addClass("processing");

  //submit ajax request
  var ajax = submitRowToBeProcessed("MassReRun", databasename, caseName, configId, partNumber, runCommand);

  //increment counter
  inProgressCount++;

  //store promise for later
  requests.push(ajax);

  ajax.then(function() {
    //this row's ajax call is complete
    row.removeClass("processing");

    //decrement counter, potentially show this number to the user
    inProgressCount--;
  });

});