<script> var second=20; function time(){ if(second<=9){second="0" + second;} if(document.getElementById){t.innerHTML=second;} if(second==00){return false;} second--; setTimeout("time()", 1000); } </script> 

The simplest version of the counter, the question is how to implement the termination of the counter failure when reloading the page, that is, 20 ... 19 ... reload ... 14 ... 15, etc.?

    1 answer 1

    As an option:

     localStorage.setItem('hello','world'); localStorage.getItem('hello'); // вернёт "world" 

    You can also: document.cookie , but I personally localStorage , I think, cookies are a relic of old browsers.

    Your code can be converted like this:

     var counter = localStorage.getItem('myCounter') || 0; setInterval(function(){ localStorage.setItem('myCounter',++counter); console.log(counter); },1000); 

    PS You can try to run directly on this site in the console, reload the page and start again. :)