使用JavaScript在HTML頁面上實現倒計時
阿新 • • 發佈:2020-12-25
技術標籤:htmljavascript
JavaScript在HTML頁面上實現倒計時
程式碼如下圖所示
<!DOCTYPE html>
<html lang="zn_CN">
<head>
<meta charset="UTF-8">
<title>頁面倒計時</title>
<script>
window.onload = function() {
var countNum=document.getElementById ("numCount");
var sec = 10;
var sa = setInterval(function(){
--sec;
countNum.innerHTML=sec + "秒"
if(sec == 0){
clearInterval(sa);
location.href="首頁.html";
}
},1000);
}
</script>
</head>
<body>
<p id="numCount">10秒</p>
</body>
</html>
執行結果: