慕课网的验证虽然过了,但是自己在浏览器上试的时候计数到0后还会继续向下,我确实没提供可以back的网页,也清楚大概是函数if执行完之后,继续向下死循环执行。但我不清楚这里面的机制,如果history.back()没有链到新的网页,原网页就会这样么?所有东西都不会被打断么?
你在if里面判断,然后跳出去不就行了
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb"/>
</head>
<body>
<!--先编写好网页布局-->
<p><span id="second">5</span>秒后回到主页<a href="window.history.back()">返回</a></p>
<script type="text/javascript">
var num=5;
//获取显示秒数的元素,通过定时器来更改秒数。
var i=setInterval("countSecond()",1000);
function countSecond(){
if(num==0){
location="";
}
document.getElementById("second").innerHTML=num;
num--;
//通过window的location和history对象来控制网页的跳转。
}
</script>
</body>
</html>
这样子写:
<!DOCTYPE html>
<html>
 <head>
  <title>浏览器对象</title>  
  <meta http-equiv="Content-Type" content="text/html; charset=gkb"/>   
 </head>
 <body>
  <!--先编写好网页布局-->
  <h1>操作成功</h1>
  <span style="font-size:25px" id="second"></span>秒后回到主页
  <a href="javascript:back()" style="font-size:25px">返回</a>
 
  <script type="text/javascript">  
 
   //获取显示秒数的元素,通过定时器来更改秒数。
    var sec=5;
    function change(){
        
        sec--;
        console.log(sec);
        if(sec==0){
            console.log(history.back());
            return history.back();
        }
        document.getElementById("second").innerHTML=sec;
        setTimeout(change,1000);
    }
    change();
    
    
   //通过window的location和history对象来控制网页的跳转。
  
 </script> 
</body>
</html>