This time I will show you how to use JS setInterval to implement a timer. What are the precautions for using JS setInterval to implement a timer? The following is a practical case, let's take a look.
UsesetInterval to implement timing, and advance one to the minute after 60 seconds, and advance one to the hour after 60 minutes.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> JS计时器</title> <script> window.onload = function(){ var mm = 0; var ss = 0; var str = ''; var timer = setInterval(function(){ str = ""; if(++ss==60) { if(++mm==60) { mm=0; } ss=0; } str+=mm<10?"0"+mm:mm; str+=":"; str+=ss<10?"0"+ss:ss; document.getElementById("d").innerHTML = str; },1000); }; </script> </head> <body> <p id="d"></p> </body> </html>
How to use Vue to implement a countdown button
How to use Vue to write a two-way data binding
The above is the detailed content of How to use JS+setInterval to implement a timer. For more information, please follow other related articles on the PHP Chinese website!