后,承诺块中的代码将被执行。
我想用sweetalert替换javascript confirm
,在下面的代码片段中,但我发现了两个限制,1)sweetalert不暂停执行,如确认,2)sweetalert不返回值,布尔值,如确认:
$(document).ready(function(){
$('#cancelList').click(function(e){
e.preventDefault();
m = confirm($(this).attr('confirm-msg'));
if (!m){
return true;
}
url = $(this).children('a').attr('href');
......
对于返回布尔值,我尝试像下面这样使用它:
$(document).ready(function(){
$('#cancelList').click(function(e){
e.preventDefault();
m = false;
swal({text:$(this).attr('confirm-msg'), type: 'warning',title: '', showCancelButton: true}, function(isConfirm){m = isConfirm; return m;});
alert('It should not be here before decide in swal');
if (!m){
return true;
}
在上面的代码片段中,alert
在sweetalert确认出现之前被调用!!全局变量m
似乎不受点击OK或取消sweetalert的影响。
有什么问题吗?是否有任何解决方案,而不是包括sweetalert回调函数内的代码的其余部分?
我也有同样的问题,在网上发现sweet alert使用promise。您可以使用sweet alert作为:
Swal.fire({
/*attributes*/
}).then(function (){
/*code to execute after alert*/
});
这样,在点击警报按钮