<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
var h = 0.01;
var s = h*60*60;
function click() {
var sh = Math.floor(s/3600);
var sm = Math.floor(s%3600/60);
var ss = Math.floor(s%60);
s--;
var p = document.getElementById("nowTime");
p.innerText = to2(sh) + ":" + to2(sm) + ":" +to2(ss);
if (s===0) {
alert("结束");
clearInterval(st);
}
}
function to2(m) {
if (m<10) {
return "0" + m;
}
return m;
}
var st = setInterval(click,1000);
</script>
</body>
<h1 id="nowTime"></h1>
<script type="text/javascript">
printTime()
</script>
</html>
注释:
-
getElementById:用id来获取对象
-
setInterval: 按照指定的周期(按毫秒计),来调用函数或计算表达式
-
clearTimeout:在指定的毫秒数后面调用函数或计算表达式(执行一次)