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

来源:8-17 编程练习

摄影祖师爷

2017-09-21 09:31


<!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>


写回答 关注

1回答

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

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

    摄影祖师爷

    非常感谢!

    2017-09-21 14:18:50

    共 1 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468692 学习 · 21895 问题

查看课程

相似问题