javascript - 在 for 循环中等待函数结果

标签 javascript angularjs for-loop sweetalert

我正在使用 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/

相关文章:

javascript - tablesorter.js 多次文本提取

angularjs - 更改 ui-view 指令处理页面转换的方式

javascript - for in Controller 中的循环

Javascript:在循环中按循环分割字符串

javascript - 查找字符串js中添加的字符

javascript - 通过数组映射时检查下一个项目属性

javascript - ajax GET 后获取重定向 url

javascript - 在指令中使用异步进行 Angular Select

javascript - 如何动态地将索引分配给 ng-show Angular 值

c - 在 for 循环中覆盖值