setInterval不起作用,可能出错?

时间:2014-06-01 17:58:11

标签: javascript jquery countdown

我创建了倒计时,这是我的一段代码,但它不起作用,我可能做错了什么?

<div id="container">
    <h1 id="time">0:00</h1>
</div>
<script type="text/javascript">
    $(document).ready(function(){
        var minutes = 1;
        var secondsRemaining = minutes * 60;
        intervalHandle = setInterval(tick,1000);
        function tick(){
            var timeDisplay = $('#time');
            var min = Math.floor(secondsRemaining / 60);
            var sec = secondsRemaining - (min * 60);
            if(sec < 0){
                sec = "0" + sec;
            }
            var message = min.toString() + ":" + sec;
            timeDisplay.html(message);
            if(secondsRemaining === 0){
                alert('Done!');
                clearInterval(intervalHandle);
                resetPage();
            }
        }
    });
</script>

`

1 个答案:

答案 0 :(得分:1)

你忘了减少每一步的秒数:-)它们仍然等于60:

secondsRemaining--;

这是小提琴; http://jsfiddle.net/GhnMr/