分别倒数

时间:2019-04-07 14:49:43

标签: javascript jquery settimeout setinterval

我正尝试为每个问题分别设置一个计时器,问题应在2分钟后显示,但我必须重复此问题以防止重新加载计时器后再启动计时器

<p>Question 1</p>
<div class="countdown"></div>
<p>Question 2</p>

var interval;
$(document).ready(function() {
    var timer2 = "2:01";
    var interval = setInterval(function loop() {
        var timer = timer2.split(':');
        var minutes = parseInt(timer[0], 10);

        var seconds = parseInt(timer[1], 10);
        --seconds;

        minutes = (seconds < 0) ? --minutes : minutes;
        if (minutes < 0) {
            clearInterval(interval);
        }

        seconds = (seconds < 0) ? 59 : seconds;
        seconds = (seconds < 10) ? '0' + seconds : seconds;

        $('.countdown').html(minutes + ':' + seconds);

        timer2 = minutes + ':' + seconds;
    }, 1000);

    setTimeout(swap, 120000);

    function swap() {
        $('html, body').animate({
            scrollTop: $(".up").offset().top - 250
        }, 700);
    }
});

有什么建议吗?

0 个答案:

没有答案