通過JavaScript倒計時

精簡版

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<script>
    var t = 9;
    function time(){
        if(t>=0){
            setTimeout(function(){
                document.getElementById("clock").innerHTML = t;
                t--;
                time();
            },1000);
        }
    }
</script>
<h1 id="clock"></h1>
<input type="button" value="倒計時開始" onclick="time()">
</body>
</html>

花裏胡哨版

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
</head>
<body>
<script>
var t = 9;
function time(){
	if(t>=0){
		setTimeout(function(){
			setStyle();
			document.getElementById("clock").innerHTML = t;
			t--;
			time();
		},1000);
	}
}
function setStyle(){
	this.r = Math.floor(Math.random()*255);
	this.g = Math.floor(Math.random()*255);
	this.b = Math.floor(Math.random()*255);
	document.body.style.color='rgb('+ this.r +','+ this.g +','+ this.b ;
}
</script>
<h1 id="clock"></h1>
<input type="button" value="倒計時開始" onclick="time()">
</body>
</html>

效果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章