我正在使用 AngularJs 和模块 ngSweetAlert http://oitozero.github.io/ngSweetAlert/#/home ,我必须在 for 循环中等待确认按钮函数中指令的执行:
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 {
}
});
}
最佳答案
编辑:正如 Andrew Luhring 所指出的,原始民意调查示例是不正确的
以下是如何使用异步回调样式在前者完成后安排每个警报弹出:
function showAlertNTimes(n) {
if (n > 0) {
SweetAlert.swal({...}, function() {
// Do confirmation stuff
showAlertNTimes(n - 1);
});
}
}
showAlertNTimes(myArray.length);
关于javascript - 在 for 循环中等待函数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870120/