0

with this code i can get what i want Count live email , limited email, and Die email but the problem is alert "finish" come first before all textarea filled completely. So i don't know if wheter loop finish or no. so i would like to wait loop till finish and all textarea filled then show some alert. anyway valid_check.php is cURL function. Hope anyone can solve my problem. Thanks

var mail_lists_clean = ["value1","value2",...];
$.each(mail_lists_clean, function(index, value){


                $.post("valid_check.php",
                    {email:value},
                    function(data,status){
                    var txt = value+"\n";
                    if(data == "live"){
                        $("textarea#live").val($("textarea#live").val() + txt); 
                        var live_length = $('textarea#live').val().split(/\r|\r\n|\n/).length;
                        $("span#live").text("Live : " + live_length);
                    }else if(data == "limited"){
                        $("textarea#limited").val($("textarea#limited").val() + txt);
                        var limited_length = $('textarea#limited').val().split(/\r|\r\n|\n/).length;
                        $("span#limited").text("Limited : " + limited_length);
                    }else{
                        $("textarea#die").val($("textarea#die").val() + txt);
                        var die_length = $('textarea#die').val().split(/\r|\r\n|\n/).length;
                        $("span#die").text("Die : " + die_length);
                    }

                    });
                });
                alert("finish");
Dark Cyber
  • 2,181
  • 7
  • 44
  • 68
  • 1
    Take advantage of the promise that is returned by each `$.post`. (looking for dupe) – Kevin B Jun 08 '15 at 21:13
  • @Roberto anyway the goal is i know that all looping finished so all textarea filled and i can do something with it :D thanks. – Dark Cyber Jun 08 '15 at 21:16
  • Kevin's suggestion is a good one. You can also do it with a simple counter that increments in your request success function. – Yogi Jun 08 '15 at 21:19
  • The linked duplicate shows both options, :) – Kevin B Jun 08 '15 at 21:19
  • 1
    Here's another, more popular one: http://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-requests-are-done?rq=1 – Kevin B Jun 08 '15 at 21:21
  • Just to clarify, the problem you are seeing is that the .each loop starts each loop one after another, but because they are making a call to an external server, it doesn't wait until each one is finished before going to the next one. So, it starts up all of them, by the time it gets to the last, only a few of them have completed. So you need to use promises or callbacks to catch the completion. There's no simple way around it, aside from the examples above. – CargoMeister Jun 08 '15 at 21:54

0 Answers0