Home > Web Front-end > JS Tutorial > JS implements real-time feedback of the current time function

JS implements real-time feedback of the current time function

php中世界最好的语言
Release: 2018-04-16 16:57:33
Original
1709 people have browsed it

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.

Use

javascriptFeedback system time

Apply knowledge

JavaScript HTML DOM

The setInterval() method in HTML DOM will continue to 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.

Syntax

setInterval(code,milliseconds)

code——code (can be a function)

milliseconds - the time (in milliseconds) of this call

Therefore, we want to make the feedback system time move. We only need to call the method 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返回给指定元素
Copy after login
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>
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:



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!

Related labels:
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template