在自执行之前等待函数完成

时间:2014-07-26 06:44:36

标签: jquery ajax function jsp servlets

我有两个函数:function1()和function2()  这两个函数都有对servlet的ajax调用。第二个函数使用来自textbox的值,该值由function1()的ajax调用更新。我的问题是在文本框更新之前运行的function2()。所以请帮助我。

我也用过

$.ajax({
    url:fun1(row),
    success:function(){
         fun2();
    }
});

 function fun1(row) {
     $(".calclr").val("0");
     var acq = row.find('td').eq(3).find("input").val();
     var rate = row.find('td').eq(6).find("input").val();
     $("#txtcomp").val(parseInt(acq * rate));
     $("#txtsolatium").val(parseInt($("#txtcomp").val() * 0.3));
     var field = $("#PlotTable").find(".tabactive");


     $(".tabactive").removeClass("tabactive");
     // $(this).children("td").addClass("tabactive");
     row.addClass("tabactive");
     var caseid = $("#caseId").val();

     JSONAry = JSON.stringify({
         Caseid: caseid
     });
     $.ajax({
         type: 'POST',
         url: 'geturgord',
         data: {
             JsonArray: JSONAry
         },
         success: function(retn) {
             var obj = JSON.parse(retn);
             if (obj.success == "true") {
                 var damg = obj.arr;
                 var objdam = JSON.parse(damg);
                 var assmnt = objdam.assess;
                 var not4us = objdam.not4us;
                 var notus417 = objdam.notus417;

                 var comland = $("#txtcomp").val();
                 if (objdam.ordurg == "1") {

                     var start = new Date(assmnt);
                     var end = new Date(not4us);
                     var diff = new Date(start - end);
                     var days = diff / 1000 / 60 / 60 / 24;

                     $("#txtintrst").val(parseInt(comland * days * (0.12 / 365)));

                 } else if (objdam.ordurg == "2") {

                     var start = new Date(assmnt);
                     var end = new Date(notus417);
                     var diff = new Date(start - end);
                     var days = diff / 1000 / 60 / 60 / 24;
                     $("#txtintrst").val(parseInt(comland * days * (0.12 / 365)));

                 }

                 var awdid = $("#hidawdid").val();
                 var pltsl = row.find('td').eq(1).find("input").val();
                 var table = "surfdam";
                 JSONArry = JSON.stringify({
                     Awdid: awdid,
                     id: pltsl,
                     Table: table
                 });

                 $.ajax({
                     type: 'POST',
                     url: 'getAwdeePlt',
                     data: {
                         JsonArray: JSONArry
                     },
                     success: function(retn) {
                         var obj = JSON.parse(retn);
                         if (obj.success == "true") {
                             var surfdam = obj.table;
                             var objdam = JSON.parse(surfdam);
                             var damtot = 0;
                             $.each(objdam, function(i) {
                                 damtot = damtot + parseFloat(objdam[i].damamount);
                             });
                             $("#txtsurf").val(parseInt(damtot));
                             $("#txttotcomp").val(parseInt($("#txtcomp").val()) +
                                 parseInt($("#txtintrst").val()) + parseInt($(
                                     "#txtsolatium").val()) + parseInt($("#txtsurf")
                                     .val()));

                         }
                     }
                 });
             }
         }
     });
 }

1 个答案:

答案 0 :(得分:0)

这一切可能最好用promises完成,但你可以用更少的重写来解决它,通过添加一个回调到fun1(),当它最终完成所有内部工作时被调用,最后数据被传递给该回调,然后您从该回调触发另一个ajax调用。

我只是在此代码中的注释后立即更改了代码:

function fun1(row, completeFn) {
     $(".calclr").val("0");
     var acq = row.find('td').eq(3).find("input").val();
     var rate = row.find('td').eq(6).find("input").val();
     $("#txtcomp").val(parseInt(acq * rate));
     $("#txtsolatium").val(parseInt($("#txtcomp").val() * 0.3));
     var field = $("#PlotTable").find(".tabactive");


     $(".tabactive").removeClass("tabactive");
     row.addClass("tabactive");
     var caseid = $("#caseId").val();

     JSONAry = JSON.stringify({
         Caseid: caseid
     });
     $.ajax({
         type: 'POST',
         url: 'geturgord',
         data: {
             JsonArray: JSONAry
         },
         success: function(retn) {
             var obj = JSON.parse(retn);
             if (obj.success == "true") {
                 var damg = obj.arr;
                 var objdam = JSON.parse(damg);
                 var assmnt = objdam.assess;
                 var not4us = objdam.not4us;
                 var notus417 = objdam.notus417;

                 var comland = $("#txtcomp").val();
                 if (objdam.ordurg == "1") {

                     var start = new Date(assmnt);
                     var end = new Date(not4us);
                     var diff = new Date(start - end);
                     var days = diff / 1000 / 60 / 60 / 24;

                     $("#txtintrst").val(parseInt(comland * days * (0.12 / 365)));

                 } else if (objdam.ordurg == "2") {

                     var start = new Date(assmnt);
                     var end = new Date(notus417);
                     var diff = new Date(start - end);
                     var days = diff / 1000 / 60 / 60 / 24;
                     $("#txtintrst").val(parseInt(comland * days * (0.12 / 365)));

                 }

                 var awdid = $("#hidawdid").val();
                 var pltsl = row.find('td').eq(1).find("input").val();
                 var table = "surfdam";
                 JSONArry = JSON.stringify({
                     Awdid: awdid,
                     id: pltsl,
                     Table: table
                 });

                 $.ajax({
                     type: 'POST',
                     url: 'getAwdeePlt',
                     data: {
                         JsonArray: JSONArry
                     },
                     success: function(retn) {
                         var obj = JSON.parse(retn);
                         if (obj.success == "true") {
                             var surfdam = obj.table;
                             var objdam = JSON.parse(surfdam);
                             var damtot = 0;
                             $.each(objdam, function(i) {
                                 damtot = damtot + parseFloat(objdam[i].damamount);
                             });
                             $("#txtsurf").val(parseInt(damtot));
                             $("#txttotcomp").val(parseInt($("#txtcomp").val()) +
                                 parseInt($("#txtintrst").val()) + parseInt($(
                                     "#txtsolatium").val()) + parseInt($("#txtsurf")
                                     .val()));
                         }
                         // call the callback to indicate that we are done now
                         completeFn(/* pass whatever data you want here */);
                     }
                 });
             }
         }
     });
 }

 // call fun1() and invoke our other ajax calls when it calls its callback 
 // to indicate that it is done with all its internal processing
 fun1(function(data) {
     $.ajax({
         url: /* I'm not sure what is supposed to go here */,
         success:function(){
             fun2();
         }
      });         
 });

仅供参考,我不确定在url字段中应该传递给你的ajax调用。您从fun1()传递了返回结果,但它没有返回任何内容,所以我不知道您打算通过那里。如果它是fun1()使用它的所有ajax调用计算的值,那么您可以将其传递给回调,它将在回调中作为参数data使用。


这个承诺版本看起来像这样:

function fun1(row) {
     $(".calclr").val("0");
     var acq = row.find('td').eq(3).find("input").val();
     var rate = row.find('td').eq(6).find("input").val();
     $("#txtcomp").val(parseInt(acq * rate));
     $("#txtsolatium").val(parseInt($("#txtcomp").val() * 0.3));
     var field = $("#PlotTable").find(".tabactive");


     $(".tabactive").removeClass("tabactive");
     row.addClass("tabactive");
     var caseid = $("#caseId").val();

     JSONAry = JSON.stringify({
         Caseid: caseid
     });
     return $.ajax({
         type: 'POST',
         url: 'geturgord',
         data: {
             JsonArray: JSONAry
         }
     }).then(function(retn) {
         var obj = JSON.parse(retn);
         if (obj.success == "true") {
             var damg = obj.arr;
             var objdam = JSON.parse(damg);
             var assmnt = objdam.assess;
             var not4us = objdam.not4us;
             var notus417 = objdam.notus417;

             var comland = $("#txtcomp").val();
             if (objdam.ordurg == "1") {

                 var start = new Date(assmnt);
                 var end = new Date(not4us);
                 var diff = new Date(start - end);
                 var days = diff / 1000 / 60 / 60 / 24;

                 $("#txtintrst").val(parseInt(comland * days * (0.12 / 365)));

             } else if (objdam.ordurg == "2") {

                 var start = new Date(assmnt);
                 var end = new Date(notus417);
                 var diff = new Date(start - end);
                 var days = diff / 1000 / 60 / 60 / 24;
                 $("#txtintrst").val(parseInt(comland * days * (0.12 / 365)));

             }

             var awdid = $("#hidawdid").val();
             var pltsl = row.find('td').eq(1).find("input").val();
             var table = "surfdam";
             JSONArry = JSON.stringify({
                 Awdid: awdid,
                 id: pltsl,
                 Table: table
             });

             return $.ajax({
                 type: 'POST',
                 url: 'getAwdeePlt',
                 data: {
                     JsonArray: JSONArry
                 }
             });
         }
     }).then(function(retn) {
         var obj = JSON.parse(retn);
         if (obj.success == "true") {
             var surfdam = obj.table;
             var objdam = JSON.parse(surfdam);
             var damtot = 0;
             $.each(objdam, function(i) {
                 damtot = damtot + parseFloat(objdam[i].damamount);
             });
             $("#txtsurf").val(parseInt(damtot));
             $("#txttotcomp").val(parseInt($("#txtcomp").val()) +
                 parseInt($("#txtintrst").val()) + parseInt($(
                     "#txtsolatium").val()) + parseInt($("#txtsurf")
                     .val()));
         }
     });
 }


 // now invoke the right sequence of functions and ajax calls
 fun1().then(function(data) {
     return $.ajax({url: /* I'm not sure what is supposed to go here */ });
 }).then(function(data) {
     fun2();
 });