jQuery倒数暂停并重复。

| 我正在尝试将某些功能编码到页面上,但是我无法将所有内容放在一起:-( 我想要的只是一个简单的计数器,该计数器计数为0,然后显示消息(.confirmation)。如果您打扰了(移动鼠标或触摸键盘),计数器将暂停并显示不同的消息(。警告),几秒钟后从头开始重复计数。 f ... 我有用于监视用户操作和转换的元素,但是我不知道如何将它们加在一起:
       //monitors user action
        $(\'.warning, .confirmation\').hide();

        $(document).bind(\'mousemove keydown\', function() {
            $(\'.warning\').show().delay(2000).fadeOut(500);
        }); 

        //counts for example from 5 seconds (this value is in the <span></span> in html section) 
        var sec = $(\'#box span\').text()
        var timer = setInterval(function() {
           $(\'#box span\').text(--sec);
           if (sec == 0) {
              $(\'.confirmation\').show(function() {
                $(document).unbind();
              });
              clearInterval(timer);
           }
        }, 1000);
    
已邀请:
尝试这样的事情:
        //countdown pause timer
        var countDownPauseTimer = 0;

        //monitors user action
        $(\'.warning, .confirmation\').hide();

        $(document).bind(\'mousemove keydown\', function() {
            countDownPauseTimer = 10; // Countdown will pause for 10 seconds
            $(\'.warning\').show().delay(2000).fadeOut(500);
        }); 

        //counts for example from 5 seconds (this value is in the <span></span> in html section) 
        var sec = $(\'#box span\').text()
        var timer = setInterval(function() {
           if (countDownPauseTimer > 0)
                countDownPauseTimer--;
           else
                $(\'#box span\').text(--sec);
           if (sec == 0) {
              $(\'.confirmation\').show(function() {
                $(document).unbind();
              });
              clearInterval(timer);
           }
        }, 1000);
我在这里所做的是引入另一个变量,以表明我们正在暂停倒计时并保留剩余的秒数。 在间隔滴答中,我检查是否有暂停并确定要减少的值... 我没有测试它,但是应该可以。 更新资料 以下是更新的代码,暂停结束后,倒计时将重新开始:
        //countdown pause timer
        var countDownPauseTimer = -1;

        //monitors user action
        $(\'.warning, .confirmation\').hide();

        $(document).bind(\'mousemove keydown\', function() {
            countDownPauseTimer = 10; // Countdown will pause for 10 seconds
            $(\'.warning\').show().delay(2000).fadeOut(500);
        }); 

        //counts for example from 5 seconds (this value is in the <span></span> in html section) 
        var sec = $(\'#box span\').text();
        var timer = setInterval(function() {
           if (countDownPauseTimer > 0)
                countDownPauseTimer--;
           else if (countDownPauseTimer == 0) {
                countDownPauseTimer--;
                sec = $(\'#box span\').text();
           }
           else
                $(\'#box span\').text(--sec);
           if (sec == 0) {
              $(\'.confirmation\').show(function() {
                $(document).unbind();
              });
              clearInterval(timer);
           }
        }, 1000);
    

要回复问题请先登录注册