- new一个Date(),setInterval()每隔一秒执行一次读取时间的函数,并innerHTML到屏幕上。很简单,但是会觉得挺有意思。
- 每秒更新一次
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
display:flex;
justify-content: center;
}
#time{
display: flex;
height: 500px;
width: 80%;
background-color: wheat;
justify-content: center;
align-items: center;
}
p{
background-color: skyblue;
font-size: 50px;
display: inline-block;
margin: auto;
}
</style>
</head>
<body>
<div id="time">
<p id="ts">time</p>
</div>
</body>
<script>
var d = new Date();
console.log(d);
function gettime(){
var t = new Date();
var tt= document.getElementById("ts");
tt.innerHTML=t;
}
setInterval(() => {
gettime();
}, 1000);
</script>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/57c64369aa416d2e66bcb879551d55c3.png)