如题,求解答.
setTimeout 是延迟多少时间后执行指定的代码,只执行一次,如果要使用setTimeout函数,那就须在要执行的函数内使用setTimeout,也须在函数外使用setTimeout
setInterval 是每隔多少时间就执行一次指定的代码
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<!--先编写好网页布局-->
<h4>操作成功</h4>
<span id="second" >5</span>
<span >秒后回到主页</span>
<a href="bk();">返回</a>
<script type="text/javascript">
var num=document.getElementById("second").innerHTML;
// document.write(num); text
function time1(){
num--;
document.getElementById("second").innerHTML=num;
setTimeout(time1,1000);
if(num==0){
window.location.href=("http://www.imooc.com");
}
}
setTimeout(time1,1000); //这里必须再执行一次setTimeout(),程序开会重复执行,否则你的程序只执行了一次。
//获取显示秒数的元素,通过定时器来更改秒数。
//通过window的location和history对象来控制网页的跳转。
</script>
</body>
</html>看最后一行的注释
内置也是可以的,但是你并没有调用函数,所以没有效果,在26行添加time1()调用函数即可。