把setInterval()里的函数单独拉出来定义,为什么5就不跳表了?是全局变量跟局部变量的问题吗?
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
</head>
<body>
<H4>操作成功</H4>
<p>
<b id="second">5</b>秒后回到主页 <a href="javascript:goBack();">返回</a>
</p>
<script type="text/javascript">
var sec = document.getElementById("second");
var i = 5;
function goHome(){
i--;
sec.innerHTML = i;
if(i==1){
window.location.href = "http://www.imooc.com/";
}
}
var timer=setInterval("goHome()"
,1000);
function goBack(){
window.history.go(-1);
}
</script>
</body>
</html>