I'm trying to send SMS to recipients from a object.
Does Jquery each loop wait for ajax success before continue?
I'd like my script to send one SMS and append a success text to DOM before continue with next recipient. When the each loop is done, i'd like to have a success message.
var i = 1;
var count = Object.keys( recipients ).length;
//LOOP threw recipients
$.each(recipients, function(nr, name){
//SEND SMS
$.ajax({
url: 'php/sms_send.php',
method: 'POST',
data: {nr: nr, name: name, text: text},
success: function(result){
//ECHO STATUS
$('<span>'+result+'</span><br>').appendTo('#recipients');
},
error: function(err){
//console.log(err);
}
}); //AJAX
//LAST ROW?
if(count === i){
//DISABLE BTN
$('#send_sms_btn').removeClass('disabled');
$('#send_sms_btn').text('Skicka');
//ECHO STATUS
$('<span class="text-success">Klar!</span><br>').appendTo('#recipients');
}
else{
i++;
}
}); //EACH
The count does not work correct, and i wonder if the each is waiting for the ajax or if it just runs?
How can i make this workflow better?