【问题标题】:How do I display a div when my timer hits zero?当我的计时器归零时,如何显示 div?
【发布时间】:2013-12-05 04:16:24
【问题描述】:

这是我的代码:

$('#TESTER').hide();  
$('#titlehead2').click(
    function() {
        var doUpdate = function() {
            $('.countdown').each(function() {
                var count = parseInt($(this).html());
                if (count !== 0) {
                    $(this).html(count - 1);
                }
            });
        };
        setInterval(doUpdate,1000);
        if(count <= 0) $('#TESTER').show();
    }
);

#TESTER 是我想要在计时器归零时显示的 div,#titlehead2 是我的计时器播放按钮。任何帮助将不胜感激。

【问题讨论】:

  • 能不能也分享一下相关的html

标签: javascript jquery timer setinterval parseint


【解决方案1】:

您需要在计时器内检查counter 的值

$('#TESTER').hide();
$('#titlehead2').click(function () {
    var doUpdate = function () {
        //need to look whether the looping is needed, if there are more than 1 countdown element then the timer logic need to be revisted
        $('.countdown').each(function () {
            var count = parseInt($(this).html());
            if (count !== 0) {
                $(this).html(count - 1);
            } else {
                $('#TESTER').show();
                //you also may want to stop the timer once it reaches 0
                clearInterval(timer);
            }
        });
    };
    var timer = setInterval(doUpdate, 1000);
});

【讨论】:

  • 非常感谢,这回答了很多问题。我很感激约翰尼先生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
相关资源
最近更新 更多