甜蜜警报会覆盖循环中的先前警报

时间:2015-05-21 07:01:23

标签: javascript jquery jquery-plugins sweetalert

我在循环内部调用甜蜜警报功能,但是sweetalert只向我显示一次,我认为它覆盖了以前的sweetalert,bcz当我做简单的警报它确实弹出两次但是甜蜜警报只是给我看一次。 我想要做的是甜蜜的警报,当我点击确定或取消按钮时显示第二或第三个警报,否则不要根据循环重复发出第二个或下一个警报 这是我的代码

$.each(data,function(index,elem){

     NotificationPopUpIsApprove();

});

function NotificationPopUpIsApprove(){
     swal({
          title: "Are you sure?",
          text: "The Task "+ task_name + " In Project " + project_name + "is Completed by " + uname ,
          type: "warning",
          confirmButtonText: "Approve!",
          closeOnConfirm: false,
          confirmButtonColor: "#44E753",
          cancelButtonText: "Reasssign!",
          showCancelButton: true,

    },
    function(isConfirm){
      if (isConfirm === true) {
        return true;
      }else{
        return false;
      }
    });

}

提前致谢..

1 个答案:

答案 0 :(得分:2)

$。每个是一个同步jQuery方法,因此它不会等待sweetalert的响应,从而触发此行为。

鉴于您的要求,希望这会有所帮助。

    var data = ['a', 'b', 'c'];
    var task_name = "sweetalert";
    var project_name = "Something";
    var uname ="SweetAlert"

    $(document).ready(function () {
        NotificationCheck(data[0]);
    });

    function NotificationCheck(noti) {
        is_last = (data.indexOf(noti) + 1 == data.length);

        NotificationPopUpIsApprove(is_last, function (boool) {
            if (boool) { // Do something with response from user
                console.log(noti);
            }

            if (data.indexOf(noti) < (data.length)) { // Inject next element for user response
                next = data.indexOf(noti) + 1;
                NotificationCheck(data[next]);
            }

        });
    }

    function NotificationPopUpIsApprove(is_last, callback) {
        swal({
            title: "Are you sure?",
            text: "The Task " + task_name + " In Project " + project_name + "is Completed by " + uname,
            type: "warning",
            confirmButtonText: "Approve!",
            closeOnConfirm: false,
            confirmButtonColor: "#44E753",
            cancelButtonText: "Reasssign!",
            showCancelButton: true,
        },

        function (isConfirm) {
            if (is_last) { //Close SweetAlert box
                swal.close();
            }
            callback(isConfirm); //Sends user's response
        });

    }