为了账号安全,请及时绑定邮箱和手机立即绑定

SweetAlert确认后关闭的问题

SweetAlert确认后关闭的问题

尚方宝剑之说 2018-08-08 07:39:41
最近用上了swal,项目需要,colseOnConfirm:true,因为“点击之后提示框不能loading住页面,不能防止用户再次点击”,所以设为true点击之后关闭,设为true问题又来了,如果执行太快里面的swal就不会弹出提示了,需要setTimeOut等待几秒钟才能弹出提示,真是蛋疼,你们有什么好的方法啊
查看完整描述

1 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

因为动画有延迟, 可以用promise,进行二次封装。

window.mconfirm = function (options) {
var prom = new Promise(function (resolve, reject) {
var swalOptions = {
title: "",
text: "",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "确认",
cancelButtonText: "取消",
closeOnConfirm: true,
closeOnCancel: true
};

if (typeof(options) == 'object') {
swalOptions = $.extend(swalOptions, options);
} else {
swalOptions = $.extend(swalOptions, {text: options});
}

swal(swalOptions, function (isConfirm) {
setTimeout(function () {
if (isConfirm) {
resolve(isConfirm);
} else {
reject(isConfirm);
}
}, 100);
});
});
return prom;
};

window.mprompt = function (options) {
var prom = new Promise(function (resolve, reject) {
var swalOptions = {
title: "",
text: "Write something interesting:",
type: "input",
showCancelButton: true,
closeOnConfirm: false,
confirmButtonText: "确认",
cancelButtonText: "取消",
inputPlaceholder: "Write something"
};

if (typeof(options) == 'object') {
swalOptions = $.extend(swalOptions, options);
} else {
swalOptions = $.extend(swalOptions, {text: options});
}

swal(swalOptions, function (inputValue) {
if (inputValue === false || inputValue === "") return false;
swal.close();
setTimeout(function () {
resolve(inputValue);
}, 100);
});
});

return prom;
};


查看完整回答
反对 回复 2018-08-20
  • 1 回答
  • 0 关注
  • 1911 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信