通过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>

效果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/The_Handsome_Sir/article/details/106409299