Tho*_*ann 5 javascript jquery sweetalert
我一直在玩SweetAlert插件:Sweet alert
我想创建一个删除按钮,在实际删除之前用户会收到提示.当用户再次按下"删除"时,则表示"已完成"并且用户必须再次单击"确定"才能提示离开.
SweetAlert有一个计时器功能,所以你可以在几秒钟左右后自动关闭最后一个"完成"提示,这很好.它们还有一个功能,您可以在用户在"完成"提示下单击"确定"时实现要运行的功能.问题是,如果定时器完成后提示自动关闭,则不运行该功能.
有什么想法可以做到这一点?
没有运行Timer和功能:
swal({
title: "Deleted!",
text: "Your row has been deleted.",
type: "success",
timer: 3000
},
function () {
location.reload(true);
tr.hide();
});
Run Code Online (Sandbox Code Playgroud)
没有计时器,但有工作功能(点击"确定"按钮):
swal("Deleted!", "Your row has been deleted.", "success"), function () {
location.reload();
tr.hide();
};
Run Code Online (Sandbox Code Playgroud)
说明
我认为你必须swal
从功能上脱颖而出.我的意思是swal
显示,功能在后台运行,模态自动关闭.
使用Javascript/jQuery的:
swal({
title: "Deleted!",
text: "Your row has been deleted.",
type: "success",
timer: 3000
});
function () {
location.reload(true);
tr.hide();
};
Run Code Online (Sandbox Code Playgroud)
您的代码包含SweetAlert的示例:
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function (isConfirm) {
if (isConfirm) {
swal({
title: "Deleted!",
text: "Your row has been deleted.",
type: "success",
timer: 3000
});
function () {
location.reload(true);
tr.hide();
};
}
else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
Run Code Online (Sandbox Code Playgroud)
你就不能用then
吗?
这种方式干净多了。
swal({
title: 'Login Success',
text: 'Redirecting...',
icon: 'success',
timer: 2000,
buttons: false,
})
.then(() => {
dispatch(redirect('/'));
})
Run Code Online (Sandbox Code Playgroud)
我找到了解决办法。
目前我正在尝试使用 sweetAlert,并找到了您问题的解决方案。
这是我用于创建 sweetalert 的自定义函数,该函数将在计时器几秒钟后关闭。
var sweetAlert = function(title, message, status, timer = 5000, isReload = false){
swal({
title : title,
text : message + '<br/>This pop up will close automatically in <strong class="swal-timer-count">' + timer/1000 + '</strong> seconds...',
type : status,
html : true,
timer : timer,
allowEscapeKey : false
}, function () {
swal.close();
if(isReload)
location.reload(true);
});
var e = $(".sweet-alert").find(".swal-timer-count");
var n = +e.text();
setInterval(function(){
n > 1 && e.text (--n);
}, 1000);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此代码调用此方法请
记住,计时器使用毫秒。
sweetAlert('Congratulation!', 'You successfully copy paste this code', 'success', 3000, false);
Run Code Online (Sandbox Code Playgroud)
小智 5
解决方案在这里。
[https://sweetalert2.github.io/]
看。带有自动关闭计时器的消息
let timerInterval
Swal.fire({
title: 'Auto close alert!',
html: 'I will close in <strong></strong> milliseconds.',
timer: 2000,
onBeforeOpen: () => {
Swal.showLoading()
timerInterval = setInterval(() => {
Swal.getHtmlContainer().querySelector('strong')
.textContent = Swal.getTimerLeft()
}, 100)
},
onClose: () => {
clearInterval(timerInterval)
}
}).then((result) => {
if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.timer
) {
console.log('I was closed by the timer')
}
})
Run Code Online (Sandbox Code Playgroud)
我的代码
swal.fire({ type: 'success', title: 'Saved.',
showConfirmButton: false, timer: 1500
}).then((result) => {
if (result.dismiss === Swal.DismissReason.timer) {
$("#new_reminder").modal("hide");
}
});
Run Code Online (Sandbox Code Playgroud)