挣扎着显示计时器

时间:2014-02-08 13:25:09

标签: javascript html timer jsfiddle

大家好,我在这里发现了一些有用的东西:

http://jsfiddle.net/4tj6r/2/

我试图按照以下方式运行,但由于某些原因我在网页上没有收到任何输出 - 我是一个完整的新手,所以如果这很简单许多道歉,但如果有任何人那么好,请指点我在正确的方向 - 代码如下:

<script type="text/javascript">
    $(function() {
        var count = localStorage.getItem('count') || 20,
            countdown = setInterval(function() {
                localStorage.setItem('count', count);
                $("p.countdown").html(count + " seconds remaining!");

                if (count == 0) {
                    clearInterval(countdown);
                    localStorage.removeItem('count');
                    window.location = 'http://stackoverflow.com/';
                }

                count--;
            }, 1000);
    });
    </script>

    <p class="countdown"></p>

非常感谢提前

1 个答案:

答案 0 :(得分:1)

你唯一想念的就是调用jQuery库

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function() {

    var count = localStorage.getItem('count') || 20;
    var countdown;
    countdown = setInterval(function() {
        --count;
       if(count>0){
           localStorage.setItem('count', count);
           $("p.countdown").html(count + " seconds remaining!");
       }else{
           clearInterval(countdown);
           localStorage.removeItem('count');
           window.location = 'http://stackoverflow.com/';  
       }
    }, 1000);

});
</script>

<p class="countdown"></p>