如何在前一个函数调用完全执行后调用javascript或jquery函数?

如何在前一个函数调用完全执行后调用javascript或jquery函数?,javascript,jquery,function,callback,synchronous,Javascript,Jquery,Function,Callback,Synchronous,我有一个简单的jquery循环。在这个循环中,我调用了一个jquery函数。但是这里发生的事情是,函数被并行调用。当上一个函数调用仍运行时,循环将再次调用相同的函数。因此,只有最后一次函数调用的输出才正确。一旦上一个函数调用完全执行后,如何调用jquery或javascript函数 这是我的循环 $("select.project_dp").each(function(index) { populate_tasks($(this)); }); 这是函数定义 function popul

我有一个简单的
jquery循环
。在这个循环中,我调用了一个
jquery函数
。但是这里发生的事情是,
函数
被并行调用。当
上一个函数
调用仍
运行时
,循环将再次调用
相同的函数
。因此,只有最后一次
函数
调用的输出才正确。一旦上一个
函数
调用完全执行
后,如何调用
jquery
javascript
函数

这是我的循环

$("select.project_dp").each(function(index) {
  populate_tasks($(this));
}); 
这是函数定义

 function populate_tasks(project_dp) {
   alert("asdfsdfasdfa");
   project_id = project_dp.val();

   next_td = project_dp.parent().next( "div" ).find("select");
   customer_div = project_dp.parent().next( "div" ).next( "div" ).find(".customer_hidden");
   start_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).find(".start_time");
   end_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).next( "div" ).find(".end_time");
   hrs_dp = project_dp.parent().next( "div" ).next( "div" ).find(".hrs_dp");
   mins_dp = project_dp.parent().next( "div" ).next( "div" ).find(".mins_dp");

    next_td.empty();
    $.ajax({
      type: "GET",
      url: '/log_times/populate_tasks',
      dataType: "JSON",
      data: {proj_id: project_id },
      success:function(data) {
    console.log(data);  
    next_td.empty();
    customer_div.val(data.cust_id) 

    jQuery.each(data.tasks,function(i, v) {
         next_td.append($('<option value="'+ data.tasks[i]["id"] +'">'+data.tasks[i]["name"] +'</option>'));


       });



       if (data.project.hrs_calc_criteria == "By Duration") {
          hrs_dp.prop('disabled', false);
          mins_dp.prop('disabled', false);
          start_time.prop('disabled', true);
          end_time.prop('disabled', true);
       } else if (data.project.hrs_calc_criteria == "By Start & End Time") { 
          hrs_dp.prop('disabled', true);
          mins_dp.prop('disabled', true);
          start_time.prop('disabled', false);
          end_time.prop('disabled', false);
       } 

      }
    });

      }
功能填充任务(项目dp){
警报(“ASDFASDFA”);
project_id=project_dp.val();
next_td=project_dp.parent().next(“div”).find(“select”);
customer_div=project_dp.parent().next(“div”).next(“div”).find(“customer_hidden”);
start_time=project_dp.parent().next(“div”).next(“div”).next(“div”).find(“start_time”);
end_time=project_dp.parent().next(“div”).next(“div”).next(“div”).next(“div”).find(“end_time”);
hrs_dp=project_dp.parent().next(“div”).next(“div”).find(“.hrs_dp”);
mins_dp=project_dp.parent().next(“div”).next(“div”).find(“mins_dp”);
next_td.empty();
$.ajax({
键入:“获取”,
url:“/log\u times/populate\u tasks”,
数据类型:“JSON”,
数据:{proj_id:project_id},
成功:功能(数据){
控制台日志(数据);
next_td.empty();
客户部门val(数据客户id)
每个(数据、任务、函数(i、v){
下一步添加($(''+data.tasks[i][“name”]+'');
});
如果(data.project.hrs\u计算标准==“按工期”){
hrs_dp.道具(“禁用”,错误);
分钟dp.道具(“禁用”,错误);
启动时间.prop('disabled',true);
结束时间.prop('disabled',true);
}else if(data.project.hrs_calc_criteria==“按开始和结束时间”){
hrs_dp.道具(“禁用”,真实);
分钟dp.道具(“禁用”,真实);
启动时间.prop('disabled',false);
结束时间.prop('disabled',false);
} 
}
});
}

传递回调函数:

function myCallback() {
    alert("Complete!");
}

$("select.project_dp").each(function(index) {
  populate_tasks($(this), myCallback);
}); 
然后,您只需在
populate_tasks()
函数结束时调用
callbackFunction

callbackFunction();
您需要使用
populate_tasks()
函数来接受新参数:

 function populate_tasks(project_dp, callbackFunction) {
   alert("asdfsdfasdfa");
   project_id = project_dp.val();

   next_td = project_dp.parent().next( "div" ).find("select");
   customer_div = project_dp.parent().next( "div" ).next( "div" ).find(".customer_hidden");
   start_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).find(".start_time");
   end_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).next( "div" ).find(".end_time");
   hrs_dp = project_dp.parent().next( "div" ).next( "div" ).find(".hrs_dp");
   mins_dp = project_dp.parent().next( "div" ).next( "div" ).find(".mins_dp");
next_td.empty();
$.ajax({
  type: "GET",
  url: '/log_times/populate_tasks',
  dataType: "JSON",
  data: {proj_id: project_id },
  success:function(data) {
console.log(data);  
next_td.empty();
customer_div.val(data.cust_id) 

jQuery.each(data.tasks,function(i, v) {
     next_td.append($('<option value="'+ data.tasks[i]["id"] +'">'+data.tasks[i]["name"] +'</option>'));


   });



   if (data.project.hrs_calc_criteria == "By Duration") {
      hrs_dp.prop('disabled', false);
      mins_dp.prop('disabled', false);
      start_time.prop('disabled', true);
      end_time.prop('disabled', true);
   } else if (data.project.hrs_calc_criteria == "By Start & End Time") { 
      hrs_dp.prop('disabled', true);
      mins_dp.prop('disabled', true);
      start_time.prop('disabled', false);
      end_time.prop('disabled', false);
   } 

//  Call your callback function when this function completes
//
callbackFunction();
}

});

}
callback\u函数
实际上只是一个指针,指向您传递的任何函数,在本例中是
myCallback


至于回调函数中应该包含的代码……这取决于您,我不知道当
populate_tasks()
函数完成时您打算做什么。

我很确定您可以使用队列或承诺来解决问题。 另一种方法可以是类似以下的补偿函数:

 var i = 0;
 var tasks = [];
 $("select.project_dp").each(function(index) {
  tasks.push($(this));
}); 

 populate_tasks(tasks[i]);

 function populate_tasks(project_dp) {
 if(i <= tasks.length){
   alert("asdfsdfasdfa");
   project_id = project_dp.val();

   next_td = project_dp.parent().next( "div" ).find("select");
   customer_div = project_dp.parent().next( "div" ).next( "div" ).find(".customer_hidden");
   start_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).find(".start_time");
   end_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).next( "div" ).find(".end_time");
   hrs_dp = project_dp.parent().next( "div" ).next( "div" ).find(".hrs_dp");
   mins_dp = project_dp.parent().next( "div" ).next( "div" ).find(".mins_dp");

    next_td.empty();
    $.ajax({
      type: "GET",
      url: '/log_times/populate_tasks',
      dataType: "JSON",
      data: {proj_id: project_id },
      success:function(data) {
    console.log(data);  
    next_td.empty();
    customer_div.val(data.cust_id) 

    jQuery.each(data.tasks,function(i, v) {
         next_td.append($('<option value="'+ data.tasks[i]["id"] +'">'+data.tasks[i]["name"] +'</option>'));


       });



       if (data.project.hrs_calc_criteria == "By Duration") {
          hrs_dp.prop('disabled', false);
          mins_dp.prop('disabled', false);
          start_time.prop('disabled', true);
          end_time.prop('disabled', true);
       } else if (data.project.hrs_calc_criteria == "By Start & End Time") { 
          hrs_dp.prop('disabled', true);
          mins_dp.prop('disabled', true);
          start_time.prop('disabled', false);
          end_time.prop('disabled', false);
       } 
                i ++;
        populate_tasks(tasks[i]);
      }
    });
}
      }
var i=0;
var任务=[];
$(“select.project_dp”)。每个(功能(索引){
任务。推送($(this));
}); 
填充_任务(任务[i]);
功能填充任务(项目dp){

if(i)“callbackFunction”中应包含哪些代码,“myCallback”中应包含哪些代码函数?无意冒犯,但代码非常混乱,在发布之前,您应该尝试将其格式化为更好的格式。我不确定这是否能解决他的问题。函数仍然被并行调用,还是我错了?这不起作用,因为$。每个都执行
populate\u tasks()
无论是否有回调。您需要移动到
populate_tasks()中的下一个索引。
它不起作用。回调正在调用,但没有任何用处。仍然会以视差方式调用函数。
 var i = 0;
 var tasks = [];
 $("select.project_dp").each(function(index) {
  tasks.push($(this));
}); 

 populate_tasks(tasks[i]);

 function populate_tasks(project_dp) {
 if(i <= tasks.length){
   alert("asdfsdfasdfa");
   project_id = project_dp.val();

   next_td = project_dp.parent().next( "div" ).find("select");
   customer_div = project_dp.parent().next( "div" ).next( "div" ).find(".customer_hidden");
   start_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).find(".start_time");
   end_time = project_dp.parent().next( "div" ).next( "div" ).next( "div" ).next( "div" ).find(".end_time");
   hrs_dp = project_dp.parent().next( "div" ).next( "div" ).find(".hrs_dp");
   mins_dp = project_dp.parent().next( "div" ).next( "div" ).find(".mins_dp");

    next_td.empty();
    $.ajax({
      type: "GET",
      url: '/log_times/populate_tasks',
      dataType: "JSON",
      data: {proj_id: project_id },
      success:function(data) {
    console.log(data);  
    next_td.empty();
    customer_div.val(data.cust_id) 

    jQuery.each(data.tasks,function(i, v) {
         next_td.append($('<option value="'+ data.tasks[i]["id"] +'">'+data.tasks[i]["name"] +'</option>'));


       });



       if (data.project.hrs_calc_criteria == "By Duration") {
          hrs_dp.prop('disabled', false);
          mins_dp.prop('disabled', false);
          start_time.prop('disabled', true);
          end_time.prop('disabled', true);
       } else if (data.project.hrs_calc_criteria == "By Start & End Time") { 
          hrs_dp.prop('disabled', true);
          mins_dp.prop('disabled', true);
          start_time.prop('disabled', false);
          end_time.prop('disabled', false);
       } 
                i ++;
        populate_tasks(tasks[i]);
      }
    });
}
      }