1.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>js动态显示时间</title>
<script src="./time_display.js"></script>
</head>
<body οnlοad="show()">
<div>
当前时间: <p id="time"></p>
</div>
</body>
</html>
2.js代码
function show()
{
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
h = check(h);
m = check(m);
s = check(s);
var obj = document.getElementById("time");
obj.innerHTML = h + ":" + m + ":" + s;
setTimeout("show()", 1000);
}
function check(x)
{
return x < 10 ? "0" + x : x;
}