顾城imooc
2015-07-24 16:51
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb"/>
</head>
<body>
<!--先编写好网页布局-->
<p><h1>操作成功</h1></p>
<h8 id="seconds"></h8>秒后回到主页<a href="windback()">返回</a>
<script type="text/javascript">
//获取显示秒数的元素,通过定时器来更改秒数。
var num = 5;
function timeCount(){
document.getElementById('seconds').innerHTML = num;
num = num-1 ;
if(num == 0 ){
window.open('http://www.imooc.com','_self');
}else{
setInterval("timeCount()",1000);
}
}
setTimeout("timeCount()");
/*function windturn(){
window.open('http://www.imooc.com','_self');
}*/
function windback(){
window.history.back();
}
//通过window的location和history对象来控制网页的跳转。
</script>
</body>
</html>为什么我的跳转时间直接从还剩4s 到2s 到 -2s 然后才跳转的,而不是按顺序5 4 3 2 1 0 然后跳转?
是的,
我已经知道了,把第20行setInterval("timeCount",1000)改为setTimeout("timeCount()",1000)就可以了!因为setTimeout要立即执行,而setInterval是间隔固定时间执行,这与程序本身的执行时间不一致,就容易出现我那种情况
JavaScript进阶篇
469056 学习 · 22582 问题
相似问题