I have each loop and after i have condition: if (flag)
The problem is that jquery runs the condition before the loop ends ( in the console i can see "console.log(flag);
" before "console.log(data);
" )
how can i run it only when the loop ends?
var flag = true;
$('.fileLink').each(function()
{
url = this.href;
var fileName = $(this).attr("data-fileName");
formData = "email= " + email + "&url=" + url + "&fileName=" + fileName;
$.ajax({
type : 'POST',
url : 'ajax/emailFiles.php',
data : formData,
dataType : 'json',
code : true
})
.done(function(data) {
console.log(data);
if ( !data.success)
flag = false;
});
});
console.log(flag);
if (flag)
$("#emailDocks_form-msg").html('<div class="alert alert-success">SUCCESS</div>');
else
$("#emailDocks_form-msg").html('<div class="alert alert-danger">ERROR</div>');