简体   繁体   中英

Javascript execute alert after all events are finished (POST request, Papaparse, Jquery Datatable Insertion)

I'm using Papaparse, and I'm executing this code. I have a counter (errorcounter) that counts how many errors are returned from an asynchronous post request. Problem is that I need to get the value of the errorcounter, and I'm testing it via alert.

I think the alert executes before the post requests of before the parsing completes. How do I execute the alert after all these execution happens: (parsing, sending post request, and displaying results in Jquery datatables.

function verifyImportComputer() {
  var errorcounter = 0;
  resetImportComputer();
  $('input[id=importFileComputer]').parse({
     config: {
        header: true,
        skipEmptyLines: true,
        step: function(results) {
           $('#importErrorCounter').html(errorcounter);
           $.each(results.data, function(index, data) {
              $.post('com/import/getcomputerimport.cfm', {importData: JSON.stringify(data)}, function(data) {
                 var dataReturn =  $.parseJSON(data);
                 var status = "";
                 jQuery.each(dataReturn.error, function() {
                    status+= "<div class='alert alert-danger'>"+this+"</div>";
                    errorcounter++;
                 });
                 ImportComputerTable.row.add([
                    dataReturn.asset_id,
                    dataReturn.asset_tag,
                    dataReturn.computer_type,
                    dataReturn.computer_name,
                    dataReturn.ip_address,
                    dataReturn.processor,
                    dataReturn.memory,
                    dataReturn.operating_system,
                    dataReturn.office,
                    dataReturn.graphics_card,
                    dataReturn.date_issued,
                    dataReturn.remarks,
                    dataReturn.is_active,
                    status
                 ]).draw(false);
                 $('#importErrorCounter').html(errorcounter);
              });
           });
        }
     }
  }); 
alert(errorcounter);
}

EDIT

I made something like this since someone posted about having callback, but nothing happens.

function verifyImportComputer() {
    errorcounter = 0;
    resetImportComputer();
    $('input[id=importFileComputer]').parse({
      config: {
         header: true,
         skipEmptyLines: true,
         step: function(results) {
            $('#importErrorCounter').html(errorcounter);
            $.each(results.data, function(index, data) {
               $.post('com/import/getcomputerimport.cfm', {importData: JSON.stringify(data)}, function(data) {
                  var dataReturn =  $.parseJSON(data);
                  var status = "";
                  jQuery.each(dataReturn.error, function() {
                     status+= "<div class='alert alert-danger'>"+this+"</div>";
                     errorcounter++;
                  });
                  ImportComputerTable.row.add([
                     dataReturn.asset_id,
                     dataReturn.asset_tag,
                     dataReturn.computer_type,
                     dataReturn.computer_name,
                     dataReturn.ip_address,
                     dataReturn.processor,
                     dataReturn.memory,
                     dataReturn.operating_system,
                     dataReturn.office,
                     dataReturn.graphics_card,
                     dataReturn.date_issued,
                     dataReturn.remarks,
                     dataReturn.is_active,
                     status
                  ]).draw(false);
                  $('#importErrorCounter').html(errorcounter);
               });
            });
         }
      }
    }, {
      function() {
         alert("haha");
         alert(errorcounter);
      }
    }); 
}

According to the documentation this should work (not tested it though so it might not - perhaps work up a JSFiddle):

function verifyImportComputer() {
    errorcounter = 0;
    resetImportComputer();
    $('input[id=importFileComputer]').parse({
            config: {
                header: true,
                skipEmptyLines: true,
                step: function(results) {
                    $('#importErrorCounter').html(errorcounter);
                    $.each(results.data, function(index, data) {
                        $.post('com/import/getcomputerimport.cfm', {
                            importData: JSON.stringify(data)
                        }, function(data) {
                            var dataReturn = $.parseJSON(data);
                            var status = "";
                            jQuery.each(dataReturn.error, function() {
                                status += "<div class='alert alert-danger'>" + this + "</div>";
                                errorcounter++;
                            });
                            ImportComputerTable.row.add([
                                dataReturn.asset_id,
                                dataReturn.asset_tag,
                                dataReturn.computer_type,
                                dataReturn.computer_name,
                                dataReturn.ip_address,
                                dataReturn.processor,
                                dataReturn.memory,
                                dataReturn.operating_system,
                                dataReturn.office,
                                dataReturn.graphics_card,
                                dataReturn.date_issued,
                                dataReturn.remarks,
                                dataReturn.is_active,
                                status
                            ]).draw(false);
                            $('#importErrorCounter').html(errorcounter);
                        });
                    });
                }
            }
        },
        complete: function() {
            function() {
                alert("haha");
                alert(errorcounter);
            }
        }
    );
}

I think I'd be inclined to add the rows all in one go rather than during each step...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM