BruceWayne BruceWayne - 5 months ago 25
AngularJS Question

wait for a function result in a for loop

I'm using AngularJs and the module ngSweetAlert http://oitozero.github.io/ngSweetAlert/#/home, i have to wait for the execution of instructions in the confirmation button function in a for loop :

for (var i = 0; i < myArray.length; i++) {
SweetAlert.swal({
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
}, function(isConfirm) {
if (isConfirm) {
// Instructions using myArray
} else {

}
});
}

Answer

EDIT: Original polling example was incorrect, as pointed out by Andrew Luhring

Here's how you could use the asynchronous callback style to schedule each alert popup after the former finishes:

function showAlertNTimes(n) {
    if (n > 0) {
        SweetAlert.swal({...}, function() {
            // Do confirmation stuff 
            showAlertNTimes(n - 1); 
        });
    }

}
showAlertNTimes(myArray.length);
Comments