我想用javascript写一个实时显示倒计时的函数,该怎么写?
2个回答
展开全部
<html>
<head>
<title></title>
<script type="text/javascript">
var total = 30, timeout;
function Countdown(){
clearTimeout(timeout);
if(total>0)
{
document.getElementById("div").innerHTML = total;
}else{
clearTimeout(timeout);
document.getElementById("div").innerHTML = 0;
return ;
}
total = total-1;
timeout = setTimeout(Countdown,1000);
}
window.onload = Countdown;
</script>
</head>
<body>
<div id="div"></div>
</body>
</html>
追问
你写的我有些地方没看明白。我修改了一下,效果也一样
var total = 30;
function Countdown()
{
if(total < 0){
return ;
}
document.getElementById("div").innerHTML = total;
total = total-1;
setTimeout("Countdown()",1000);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询