慕码人4983529
2018-07-16 10:54
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb"/>
</head>
<body>
<!--先编写好网页布局-->
<h1>操作成功!</h1>
<span id="one">5</span>
<span>s后返回主页</span>
<a href="javascript:back();">返回</a>
<script type="text/javascript">
//获取显示秒数的元素,通过定时器来更改秒数。
var num=document.getElementById('one').innerHTML;
function a()
{
document.getElementById('one').innerHTML=num;
num--;
if(a==0)
{
location.assign("www.imooc.com");
}
}
setTimeout(a(),1000);
//通过window的location和history对象来控制网页的跳转。
function b()
{
window.history.back();
}
</script>
</body>
</html>
setTimeout(a(),1000);a()加引号,setTimeout改成setInterval
setTimeout是延迟多少长时间来执行代码。而你需要动态了执行这段代码需要使用setInterval("a()",1000);这个函数
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb"/>
</head>
<body>
<!--先编写好网页布局-->
<h1>操作成功!</h1>
<span id="one">5</span>
<span>s后返回主页</span>
<a href="javascript:b();">返回</a>
<script type="text/javascript">
//获取显示秒数的元素,通过定时器来更改秒数。
var num=document.getElementById('one').innerHTML;
function a()
{
--num;
document.getElementById('one').innerHTML=num;
if(num>0){
if(num==1)
{
location.assign("www.imooc.com");
}
}
else{
clearInterval("a()");
}
}
setInterval("a()",1000);
//通过window的location和history对象来控制网页的跳转。
function b()
{
window.history.back();
}
</script>
</body>
</html>
JavaScript进阶篇
468726 学习 · 22053 问题
相似问题