当我的计时器达到零时,如何显示div?

时间:2013-12-05 04:16:25

标签: javascript jquery timer setinterval parseint

这是我的代码:

$('#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是我的计时器播放按钮。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您需要检查计时器中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);
});