I'm trying to trigger several actions when clicking a button .button
I first need to clean each <div> containing a class .classToClean and launch some ajax scripts to update the database. To do that I choose the $(.classToClean).each(function(index){}); structure and then the ajax $.ajax({}) structure. So far my code look like this.
$(".button").on("click", function () {
///// each loop /////
$( ".classToClean" ).each(function( index ) {
$.ajax({
//... (clean 1)
success:function(response){
$.ajax({
//... (clean 2)
success:function(response){
}
});
}
});
});
///// /each loop /////
});
Once it's clean I want to write the new statement to the database but this time I only need ajax. As a result I got something like this:
$(".button").on("click", function () {
///// each loop /////
$( ".classToClean" ).each(function( index ) {
$.ajax({
//... (clean 1)
success:function(response){
$.ajax({
//... (clean 2)
success:function(response){
}
});
}
});
});
///// /each loop /////
$.ajax({
//... (write 1)
success:function(response){
$.ajax({
//... (write 2)
success:function(response){
}
});
}
});
});
Everything works fine but I would like to refresh when the each() and the ajax are done. Usually I only got one script so I add a success function like this
success:function(response){
location.href = "...";
},
But because I got a lot of scripts to run it's seems there are no good place to put it. For example, if I put it in the "write 2" success function sometimes it seems the .each() has not finished and so the page refresh before some scripts are executed.
Hence my question: Is there a way to refresh only when all the scripts are executed in the .each and the ajax ?