简体   繁体   English

sweetalert2 取消计时器/超时

[英]sweetalert2 cancel timer/timeout

I wanted to make a cancelable self-closing modal after 5 seconds with SweetAlert2 but I could't find a way to stop the timer, please help!我想在 5 秒后使用 SweetAlert2 制作一个可取消的自动关闭模式,但我找不到停止计时器的方法,请帮忙! Here's the js code:这是js代码:

$(document).on('click', '#view_more_trigger', function(e) { 
    e.preventDefault();
    var div = document.getElementById('view_more');
    div.style.display = div.style.display == 'none' ? 'block' : 'none';
    $(this).html( div.style.display == 'none' ? "View Moar" : "View Less");
});

var closeInSeconds=10,
    timerInterval;

var my_swal = swal({
    type:   'success',
    html:   '<div id="view_more" style="display: none;">'+
                $('#details').html() +
            '</div>'+
            '<a href="javascript:;" id="view_more_trigger" >View more</a>'+
            '<p id="show_timeout" style="display: none;"></p>',
    timer: closeInSeconds * 1000,

    onOpen: () => {

        $('#show_timeout').show();
        timerInterval = setInterval(function() {

            $(document).on('click', '#view_more_trigger', function(e) { 
                e.preventDefault();

                console.log('trying desperately to cancel the damn timer!');
                //swal.stop();                //  error, function !exists
                //my_swal.timeout.stop();     //  Cannot read property 'stop' of undefined
                //my_swal.timer.stop();       //  Cannot read property 'stop' of undefined


                // ..or to increase it
                if(closeInSeconds<1000) {
                    console.log('incrementing closeInSeconds by 1000');
                    closeInSeconds +=1000;
                }
                //swal.timer = closeInSeconds * 1000;

                swal.timer = closeInSeconds * 1000;
                my_swal.timer = closeInSeconds * 1000;

                clearInterval(timerInterval);
            });

            closeInSeconds--;
            if (closeInSeconds < 0) {
                clearInterval(timerInterval);
            }

            swal.getContent().querySelector('#show_timeout').textContent = (Math.round(swal.getTimerLeft()/1000)*1000)/1000 + 's';

        }, 1000);
    },
    onClose: () => {
        clearInterval(timerInterval)
    },

    showConfirmButton: false,
});

The only function to work with timer I found is Swal.getTimerLeft().我发现与计时器一起使用的唯一函数是 Swal.getTimerLeft()。

I created a pen in https://codepen.io/teoui/pen/MzdzWy to play with我在https://codepen.io/teoui/pen/MzdzWy创建了一支笔来玩

Thanks!谢谢!

I'm the author of SweetAlert2, and a new version with Swal.stopTimer() support was just released.我是 SweetAlert2 的作者,刚刚发布了支持Swal.stopTimer()的新版本。

Here's how you can use it:使用方法如下:

 Swal.fire({ title: 'Auto close alert!', html: 'I will close in 5 seconds. <a href id="stop-timer">Stop timer</a>', timer: 5000, didOpen: () => { Swal.getHtmlContainer().querySelector('#stop-timer').addEventListener('click', e => { e.preventDefault() Swal.stopTimer() }) } })
 <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM