想要实现当计时器到0的时候不在继续进行循环,使其出现负数。 为什么我的代码实现不了?
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb"/>
</head>
<body>
<!--先编写好网页布局-->
<h1>操作成功</h1>
<span id="time">5</span><span>秒后会到主页<a href="javascript:back()">返回</a></span>
<script type="text/javascript">
var timer=document.getElementById("time").innerHTML;
//获取显示秒数的元素,通过定时器来更改秒数。
function change(){
timer--;
document.getElementById("time").innerHTML=timer;
if(timer==0){
var i=setInterval("change()",1000);
clearInterval(i);
window.open("http://www.imooc.com/");
}
}
setInterval("change()",1000);
function back(){
window.history.back()
}
//通过window的location和history对象来控制网页的跳转。
</script>
</body>
</html>