kevine099
2016-04-14 13:41
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p style="font-size: 24px;font-weight: bold;">操作成功</p>
<p style="font-size: 20px"><span id="i">5</span>秒后返回 <a href="javascript:window.history.back();">返回</a></p>
<script type="text/javascript">
clock();
var j=5;
function clock(){
document.getElementById("i").innerHTML=j;
j--;
if(j==0){window.location.href="http://www.imooc.com/"}
setTimeout(clock,1000);
}
//通过window的location和history对象来控制网页的跳转。
</script>
</body>
</html>为什么加载后最先显示的是undefined秒,然后才开始5秒倒计时呢?明明我span标签内预先设置了5秒啊??
想请教你一个问题,j--是j自减,但是如何保证它一秒钟往下减一?谢谢.......
我知道了应该把j=5放在clock()调用前面,否则函数体里中j没有值,所以会出现undefined,若是这样span标签内可以不用写5。
第二种方法就是span标签内写出内容5,然后j=document.getElementById("i").innerHTML,将值赋值给j,然后再进行clock调用
JavaScript进阶篇
469062 学习 · 22582 问题
相似问题