Question

Hi all I found a useful bit of JS here:

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

and I am trying to run this as follows but, for some reason I am receiving no output on the webpage - I am a complete newbie so if this is simple many apologies but if there is anyone kind enough out there please point me in the right direction - the code is as follows:

<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>

many thanks in advance

Was it helpful?

Solution

All you miss is to call the jQuery library

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top