【问题标题】:How to add timer after sweetalert2 open?sweetalert2打开后如何添加定时器?
【发布时间】:2017-04-10 07:36:12
【问题描述】:

我想在Sweetalert2 中回调另一个函数后添加计时器。 在示例中,只需在 swal 运行时添加计时器。

这是基本代码:

swal({
  title: 'alert!',
  text: 'I need this close after my function',
})

下面是ajax,我需要在second 中关闭swal ajax 成功

$.ajax({
    dataType: "json",
    url: url,
    type: "GET",
    data: {some: data},
    success: function (json) {
        if (json.status === 'OK') {
            swal({
                timer:2000
            });
        }
    }
});

但是当ajax成功时,swal()的新模态用定时器打开,而不是关闭当前swal()

【问题讨论】:

  • 提供你写的代码行
  • 我已经完成了代码
  • 当然,由于您再次使用 swal() 函数,它会打开一个新模式
  • 有什么想法吗?
  • 更新了我的答案。检查这是否适合您

标签: javascript alert sweetalert sweetalert2


【解决方案1】:

在 ajax 成功时隐藏当前模式。试试这个

$.ajax({
dataType: "json",
url: url,
type: "GET",
data: {some: data},
success: function (json) {
    if (json.status === 'OK') {
        hideSwal()
    }
}});
function hideSwal(){
setTimeout(function(){
  var button = document.getElementsByClassName('swal2-confirm')[0]
  button.click()
},2000)
}

setTimeout(function(){
  var button = document.getElementsByClassName('swal2-confirm')[0]
  button.click()
},2000)
swal('Any fool can use a computer')
<script src="https://cdn.jsdelivr.net/sweetalert2/6.6.0/sweetalert2.min.js"></script>

【讨论】:

  • 感谢您的回答,但我需要一些 function 与 ajax 并成功,swal() 由计时器关闭..
【解决方案2】:

这受到 Gaurav Chaudhary 的回答的启发。使用 swal doc 标准修改的 ID。我发布这个是因为他不接受我的编辑。

//if(swal.isVisible()) { // uncomment to check if modal swal visible
    setTimeout(function(){
        swal.closeModal();
     }, 2000);
//}


swal('Any fool can use a computer')
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/sweetalert2/6.6.0/sweetalert2.min.css">
<script src="https://cdn.jsdelivr.net/sweetalert2/6.6.0/sweetalert2.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多