I need to execute som code after a jQuery .each
I have tried just to wirte it after the each loop, but then it executes immediately. I have tried with .promise().done, but that gives me an error that promise() is not a function.
My code
$("#loader").show();
var url = $(this).attr("action");
var formData = new FormData($(this)[0]);
$(this).hide();
$.ajax({
url: "generateJson.php",
type: 'POST',
data: formData,
async: true,
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (data, status)
{
$('#json').append(data); //content loads here
},
error: function (xhr, desc, err)
{
console.log("error");
}
}).done(function( data ) {
data = JSON.parse( $("#json").val() );
//console.log(data.length);
$.each( data, function( key, val ) {
console.log(val);
//$("#result").append(val);
$.ajax({
type: "POST",
url: "/generateInvoice.php",
dataType: "html",
data: val,
success: function (data, status) {
//console.log(data);
if (data) {
$("#result").append(data)
// console.log("Hurray");
} else {
console.log("Error");
}
},
error: function (xhr, desc, err)
{
console.log("error: " + desc + " " + err);
}
});
}).promise().done(function(){
$("#loader").hide();
$("#done").show();
});
});