It is necessary to make a count of the specified number of seconds when clicking on the button. It is necessary to count out 20 seconds, but also hundredths of a second should be considered.

Timer block code:

<div class="timer">00:<span class="seconds">20</span>:<span class="centisecond">00</span></div> 

For seconds, I did this:

 function timer() { var seconds = 20; var seconds_timer_id = setInterval(function() { if (seconds > 0) { seconds --; if (seconds < 10) { seconds = "0" + seconds; } $(".seconds").text(seconds); } else { clearInterval(seconds_timer_id); } }, 1000); } 

How to make hundredths of a second?

  • instead of 1000, write 10, this is the hundredth part of a second - Jean-Claude

1 answer 1

You can do this:

 function timer() { var msecond = 2000; var seconds_timer_id = setInterval(function() { seconds = Math.floor(msecond/100); centisecond = Math.floor(msecond - seconds*100); if (msecond >= 0) { msecond --; if (centisecond < 10) { centisecond = "0" + centisecond; } $(".centisecond").text(centisecond); if (seconds < 10) { seconds = "0" + seconds; } $(".seconds").text(seconds); } else { clearInterval(seconds_timer_id); } }, 10); }