问答详情
源自:8-17 编程练习

为什么一定设置定时器,那个秒数才会变化?


<!DOCTYPE html>

<html>

 <head>

  <title>浏览器对象</title>  

  <meta http-equiv="Content-Type" content="text/html; charset=gkb"/>   

 </head>

 <body>

  <!--先编写好网页布局-->

  <p>操作成功</p>

  <span id="second">5</span><span>秒后回到主页</span><a href="http://www.imooc.com/qadetail/236877">返回</a>

  <script type="text/javascript">  

    var second=document.getElementById("second").innerHTML;

    function method(){

        second--;

        document.getElementById("second").innerHTML=second;

        if(second==0){

            location.assign("http://www.imooc.com/qadetail/236877");

        }

    }

   //获取显示秒数的元素,通过定时器来更改秒数。

     setInterval(method,1000);

   //通过window的location和history对象来控制网页的跳转。

   

 </script> 

</body>

</html>


提问者:摄影祖师爷 2017-09-21 09:31

个回答

  • 慕前端7539174
    2017-09-21 10:31:10
    已采纳

    因为那个定时器的意思是,过多少时间执行一次函数,执行一次函数method()那个数字才会发生变化。