This time I will bring you JS to realize the real-time feedback of the current time function. What are the precautions for JS to realize the real-time feedback of the current time function? The following is a practical case, let’s take a look.
UsejavascriptFeedback system time
Apply knowledge JavaScript HTML DOM The setInterval() method in HTML DOM will continue to call the SyntaxsetInterval(code,milliseconds)
setInterval(function(){myTimer()},1000)
new Date()//获得当前时间 .toLocaleTimeString()//根据本地时间把Date对象的时间部分转换为字符串 .getElementById("clock")//返回带有指定 ID 的元素 .innerHTML=c; //将c返回给指定元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p id="clock"></p> </body> <script> var a = setInterval(function(){myTimer()},1000); function myTimer(){ var b = new Date(); var c = b.toLocaleTimeString(); document.getElementById("clock").innerHTML=c; } </script> </html>
The above is the detailed content of JS implements real-time feedback of the current time function. For more information, please follow other related articles on the PHP Chinese website!