each wait until finish $.ajax , and then continue

前端 未结 4 980
余生分开走
余生分开走 2021-01-02 07:22
    function genTask(elem){
    elem.each(function(){
        $this=$(this).parent(\'.cntTasks\');
        var pattern=/taskId-(.*)$/
        var idTask=$this.attr(\         


        
相关标签:
4条回答
  • 2021-01-02 07:54

    Option 1: Switch to next element in your array in the success handler.

    Option 2: Make ajax requests synchronously:

    • global:

       $.ajaxSetup({ async: false });
      
    • or directly in the request:

       $.ajax({
           async: false,
           type: "POST",
           url: domain+"/view_tasks/gen_tasks/",
           dataType: 'html',
           data: data,
           success: function(dt){
              $this.find('.contChildTasks').html(dt);
              childs = $this.children('.taskDesc').find('.has_child');
              if(childs.length != 0) {
                  genTask(childs);
              }
           }
      });
      
    0 讨论(0)
  • 2021-01-02 07:59

    In your $.ajax call add async: false and it will send a blocking request.

    0 讨论(0)
  • try putting ajaxsetup({async:false}); before your each loop then after the loop reset it back to true so your future ajax request can still be async

    0 讨论(0)
  • 2021-01-02 08:11

    Set async to false on the $.ajax call.

    0 讨论(0)
提交回复
热议问题