Home  >  Article  >  Web Front-end  >  Real-time feedback of system time using pure JavaScript

Real-time feedback of system time using pure JavaScript

小云云
小云云Original
2017-12-09 14:18:011513browse

JavaScript is a very useful language. In this article, we mainly introduce the relevant information on realizing real-time feedback system time in pure JavaScript. Friends who need it can refer to it. I hope it can help everyone.

Feedback system time using javascript

Use knowledge

JavaScript HTML DOM

The setInterval() method in HTML DOM will continuously call the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.

SyntaxsetInterval(code,milliseconds)

code——code (can be a function)

milliseconds——the time of this call (milliseconds) )

Therefore, we want the feedback system time to move. We only need to make the method call every 1000 milliseconds to make the displayed time move like an alarm clock.

setInterval(function(){myTimer()},1000)

new Date()//获得当前时间
.toLocaleTimeString()//根据本地时间把Date对象的时间部分转换为字符串
.getElementById("clock")//返回带有指定 ID 的元素
.innerHTML=c; //将c返回给指定元素

Code part

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

Related recommendations:

js, html method of obtaining the current system time

Several beautiful timeline tutorials implemented with Jquery

How to get time in JS

The above is the detailed content of Real-time feedback of system time using pure JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn