i==0后页面应该就直接跳转了,但还是会执行之后的语句,在页面显示”0秒“(加上else才可以避免),这是什么原因呢?
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<!--先编写好网页布局-->
<h1>操作成功</h1>
<p>
<span id="timer">5</span>秒后返回主页<a href=window.history.back()>返回</a>
</p>
<script type="text/javascript">
//获取显示秒数的元素,通过定时器来更改秒数。
var i = document.getElementById("timer").innerHTML;
var t=0;
function setTimer()
{
i--;
if(i==0){
clearInterval(t);
window.location.replace("http://www.imooc.com/");
}
//else{
document.getElementById("timer").innerHTML=i;
//}
}
t = setInterval(setTimer,1000);
</script>
</body>
</html>