因为动画有延迟, 可以用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;
};
谢谢了,我先看看
@求知若饥,虚心若愚: Promise 异步对象也可用 jquery 延迟对象替换。