1 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
因?yàn)閯?dòng)畫有延遲, 可以用promise,進(jìn)行二次封裝。
window.mconfirm = function (options) {
var prom = new Promise(function (resolve, reject) {
var swalOptions = {
title: "",
text: "",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "確認(rèn)",
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: "確認(rèn)",
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;
};
添加回答
舉報(bào)