SweetAlert在屏幕上时停止控制

时间:2016-11-01 15:01:01

标签: javascript jquery sweetalert

我正在使用sweetalert2库在我的代码中显示警告。

ConfirmationMessage = function(msg) {
    swal({
            title: "",
            text: msg,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Ok",
            cancelButtonText: "Cancel",
            closeOnConfirm: false,
            closeOnCancel: false,
            allowEscapeKey: true

        });
}

这是JS功能,我在其他地方使用它。

if (!ConfirmationMessage("message to show.")) {
    alert("if");           
}
else {
    alert("else");
}

我的问题是

  

我想在屏幕上显示警报时停止控制,如果确定如果条件,如果取消,则想要决定按钮按下strong>来到 else条件但是控件不等待sweetalert2中的响应。

1 个答案:

答案 0 :(得分:3)

创建swalasynchronous process, meaning you cannot just return a synchronous result from it

如果您查看docs,就可以看到swal返回promise,因此您可以利用这一点并传递成功和失败的回调:

ConfirmationMessage = function(msg) {
  return swal({ ... }); // <--- return the swal call which returns a promise
};

ConfirmationMessage('message to show')
  .then(function() {
    // success happened
  }, function(dismiss) {
    // fail happened
    // dismiss can be 'cancel', 'overlay', 'close', and 'timer'
    if (dismiss === 'cancel') {
      // user cancelled
    }
  });