if(num==1)还是if(sum==0)???
为什么是if(num==1)呀,不应该是if(num==0)时跳转吗,可是当写==0时,计时器变为-1才跳转,麻烦朋友们给讲一下,谢谢了
2015-07-23
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb"/>
</head>
<body>
<!--先编写好网页布局-->
<h2>操作成功</h2>
<p><span id="second">5</span>秒后回到主页<a href="javascript:back()">返回</a></p>
<script type="text/javascript">
var num=document.getElementById("second").innerHTML;
//获取显示秒数的元素,通过定时器来更改秒数。
function count(){
num--;
document.getElementById("second").innerHTML=num;
if(num==1){
location.assign("http://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp");
}
}
setInterval("count()",1000);
function back(){
window.history.back();
}
//通过window的location和history对象来控制网页的跳转。
</script>
</body>
</html>想了一下原因,可能是由于if内部跳转网页速度不够快,导致函数继续执行,num减为-1被你看到了。这个例子的URL是需要翻墙才可以看到的,所以加载速度特别慢,反正最后是无法访问,但可以看到不仅是变成了-1,-2。
举报