Javascript 防止关闭SweetAlert
我有一个SweetAlert弹出窗口来确认一个操作,我需要找到一种方法来稍微调整它以改善用户体验 首先,我有一个启动SweetAlert的按钮,用于接收申请者并向他们的电子邮件地址发送电子邮件。现在发送电子邮件需要一些时间,比如几秒钟 我所做的是在触发SweetAlert的按钮上添加了一个微调器,但问题是,用户并没有注意到它,因为触发SweetAlert会向上滚动整个页面,而触发它的按钮位于下方 所以我想在SweetAlert上的confirm按钮上添加微调器,但是默认情况是在单击confirm按钮后,SweetAlert关闭 这是我的密码:Javascript 防止关闭SweetAlert,javascript,jquery,sweetalert,sweetalert2,Javascript,Jquery,Sweetalert,Sweetalert2,我有一个SweetAlert弹出窗口来确认一个操作,我需要找到一种方法来稍微调整它以改善用户体验 首先,我有一个启动SweetAlert的按钮,用于接收申请者并向他们的电子邮件地址发送电子邮件。现在发送电子邮件需要一些时间,比如几秒钟 我所做的是在触发SweetAlert的按钮上添加了一个微调器,但问题是,用户并没有注意到它,因为触发SweetAlert会向上滚动整个页面,而触发它的按钮位于下方 所以我想在SweetAlert上的confirm按钮上添加微调器,但是默认情况是在单击confirm
$("#accept_button").click(function(e){
Swal.fire({
title: 'Are you sure you want to accept this applicant?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Accept Applicant'
}).then((result) => {
if (result.value) {
console.log($(this).data('url'));
$.ajax({
type: 'POST',
url: $(this).data('url'),
data: {key:$(this).data('key'),email:$(this).data('email')},
beforeSend: function() {
$('#accept_button').attr("disabled", true);
$("#accept_button").addClass("m-loader m-loader--light m-loader--left");
},
success: function(data) {
console.log(data);
if (data.ret) {
console.log('accepted');
Swal.fire(
'Success!',
'Applicant has been accepted.',
'success'
)
var delay = 1000;
setTimeout(function(){ window.location = data.new_url; }, delay);
} else {
Swal.fire(
'Failed!',
'Error occured.',
'warning'
)
}
},
error: function(xhr) {
$('#accept_button').attr("disabled", false);
$("#accept_button").removeClass("m-loader m-loader--light m-loader--left");
toastr.options.timeOut = 5000;
toastr.error('Error occured.', 'Error');
},
dataType: 'json'
});
}
})
});
我希望你们能帮助我和我的网络应用的用户。多谢各位 为什么不使用“自动关闭计时器”,这样您就可以向用户显示消息并吸引用户的注意力:
let timerInterval
Swal.fire({
title: 'Auto close alert!',
html: 'I will close in <strong></strong> milliseconds.',
timer: 2000,
willOpen: () => {
Swal.showLoading()
timerInterval = setInterval(() => {
Swal.getHtmlContainer().querySelector('strong')
.textContent = Swal.getTimerLeft()
}, 100)
},
didClose: () => {
clearInterval(timerInterval)
}
}).then((result) => {
if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.timer
) {
console.log('I was closed by the timer')
}
})
let timerInterval
喷火({
标题:“自动关闭警报!”,
html:“我将在毫秒后关闭。”,
计时器:2000,
willOpen:()=>{
Swal.showLoading()
timerInterval=setInterval(()=>{
Swal.getHtmlContainer().querySelector('strong')
.textContent=Swal.getTimerLeft()
}, 100)
},
didClose:()=>{
清除间隔(timerInterval)
}
})。然后((结果)=>{
如果(
/*阅读下面有关处理解雇的更多信息*/
result.dismise===Swal.DismissReason.timer
) {
console.log('我被计时器关闭')
}
})
您可以控制计时器,甚至阻止UI执行任何其他操作。请看“带有自动关闭计时器的消息”示例: