首页 > 代码库 > js数字时钟
js数字时钟
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>Digital Clock</title>
<style>
#clock{
font: bold 24pt sans;
background-color:#ddf;
padding:10px;
border:2px solid black;
border-radius:10px;
}
</style>
<script>
function displayTime(){
var elt = document.getElementById("clock");
var now = new Date();
elt.innerHTML = now.toLocaleTimeString();
/*toLocaleTimeString() 方法可根据本地时间把 Date 对象的时间部分转换为字符串,并返回结果。*/
setTimeout(displayTime,1000) /*每过一秒执行一次displayTime*/
}
window.onload = displayTime;
</script>
</head>
<body>
<h1>Digital Clock</h1>
<span id="clock"></span>
</body>
</html>
js数字时钟