11
votes

chaque attendez jusqu'à la fin de $ .ajax, puis continuez

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


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

Peut-être que vous pourriez définir l'attribut async de $ .ajax à false ? Voir: API.JQUERY.com/JQUERY.AJAX


4 Réponses :


0
votes

Essayez de mettre ajaxsetup ({async: false}); avant votre boucle Ensuite, après la réinitialisation de la boucle, votre future demande AJAX peut toujours être async


0 commentaires

23
votes

Option 1: Passez à l'élément suivant de votre tableau dans le succès gestionnaire .

Option 2: Faites des demandes AJAX de manière synchrone:

  • global: XXX

  • ou directement dans la demande: XXX


0 commentaires

0
votes

SET async sur false sur le $. Ajax appel.


0 commentaires

0
votes

dans votre $. Ajax appelle Ajouter async: false et il enverra une demande de blocage.


0 commentaires