Uza*_*ade 2 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
});
}
Run Code Online (Sandbox Code Playgroud)
这是JS函数,我在其他地方使用它。
if (!ConfirmationMessage("message to show.")) {
alert("if");
}
else {
alert("else");
}
Run Code Online (Sandbox Code Playgroud)
我的问题是
我想在屏幕上出现警报时停止控件,并且想要决定按下按钮(如果“确定”到达“If 条件” ,如果“取消”到达其他条件),但控件不会等待 sweetalert2 中的响应。
创建swal是一个异步过程,这意味着您不能只从中返回同步结果。
如果您查看文档,您可以看到它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
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3464 次 |
| 最近记录: |