编程语言
首页 > 编程语言> > JavaScript-在屏幕上显示SweetAlert时停止控件

JavaScript-在屏幕上显示SweetAlert时停止控件

作者:互联网

这个问题已经在这里有了答案:            >            How do I return the response from an asynchronous call?                                    35个
我正在使用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");
}

我的问题是

I want to stop the control when the alert is on screen and want to decide on the button push if OK come to If Condition if Cancel come to else condition but control does not wait for the response in sweetalert2.

解决方法:

创建swal是asynchronous 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
    }
  });

标签:sweetalert,javascript,jquery
来源: https://codeday.me/bug/20191118/2024870.html