每次等待直到完成$ .ajax,然后继续 [英] each wait until finish $.ajax , and then continue

查看:91
本文介绍了每次等待直到完成$ .ajax,然后继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    function genTask(elem){
    elem.each(function(){
        $this=$(this).parent('.cntTasks');
        var pattern=/taskId-(.*)$/
        var idTask=$this.attr('id').match(pattern);
        var data='id_task='+idTask[1];
        if(typeof jsVar2 !='undefined') data+=jsVar2;
        $.ajax({
             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);
                }
             }
        });
        $this.find('.taskDesc').show();

    });
}

if(typeof jsVar2 !='undefined') genTask($('.cntTasks .has_child'));


});    

怎么可能使$.each等到动作$.ajax将完成,然后继续循环,我无法获得$ this var,因为它具有最后一个值,对不起我的英语,谢谢! !!

how is possible to make $.each to wait until action $.ajax will be finished , and then continue loop , i cannot get $this var , because it has the last value , sorry for my English , THANK YOU !!!!

推荐答案

选项1:在success处理程序中切换到数组中的下一个元素.

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

选项2:同步发出ajax请求:

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);
            }
         }
    });
    

  • 这篇关于每次等待直到完成$ .ajax,然后继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆