为什么我的秒数只跳了一次

来源:8-17 编程练习

Ticvory

2016-11-02 18:30

<!DOCTYPE html>
<html>
 <head>
  <title>浏览器对象</title>  
  <meta http-equiv="Content-Type" content="text/html; charset=gkb"/>   
 </head>
 <body>
  <!--先编写好网页布局-->
  <h2>操作成功</h2>
  <p><span id="ctime" >5</span>回到主页<a href="back()">返回<a/></p>
 
  <script type="text/javascript"> 
    var index = window.location.host;
    var num = document.getElementById("ctime").innerHTML;
    function count(){
        num = num - 1;
        document.getElementById("ctime").innerHTML=num;
        if(num == 0){
        location.assign("www.imooc.com") ;
        }
    }//获取显示秒数的元素,通过定时器来更改秒数。
    setTimeout("count()",1000);
    function back(){
        window.history.back()
    }
   //通过window的location和history对象来控制网页的跳转。
   
 </script> 
</body>
</html>


写回答 关注

4回答

  • 一直都在路上
    2016-11-02 19:46:28
    已采纳

    你要用setInterval() 同学 setTimeout 只会触发一次

    Ticvor...

    非常感谢!

    2016-11-10 21:22:56

    共 4 条回复 >

  • 沐皓
    2016-11-09 22:46:26

    里面最后加一个setTimeout

  • colg
    2016-11-02 20:27:56
    <!DOCTYPE html>
    <html>
    <head>
    	<title>浏览器对象</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=gkb" />
    </head>
    <body>
    	<!--先编写好网页布局-->
    	<h2>操作成功</h2>
    	<p><span id="ctime" >5</span>回到主页<a href="back()">返回</a></p>
    
    	<script type="text/javascript">
    		var index = window.location.host;
    		var num = document.getElementById("ctime").innerHTML;
    
    		function count() {
    			num = num - 1;
    			document.getElementById("ctime").innerHTML = num;
    			if (num == 0) {
    				location.assign("www.imooc.com");
    			}
    		}
    
    		//获取显示秒数的元素,通过定时器来更改秒数。
    		//setTimeout不会重复
    		//setTimeout("count()", 1000);
    
    		//SetInterval为自动重复
    		setInterval("count()", 1000);
    
    		//通过window的location和history对象来控制网页的跳转。
    		function back() {
    			window.history.back()
    		}
    	</script>
    </body>
    </html>


  • Dengju
    2016-11-02 20:06:20

    <!DOCTYPE html>

    <html>

     <head>

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

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

     </head>

     <body>

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

      <p>操作成功</p></br>

      <span id="count">5</span>

        <p>秒后回到主页

            <input type="button" value="返回" onclick="goback()">

        </p>

        </br>

     

      <script type="text/javascript">  

        var num=document.getElementById("count").innerHTML;

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

         function startCount(){

             document.getElementById("count").innerHTML=num;

             num=num-1;

             if(num==0)

               location.assign("www.imooc.com");

          }

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

          setInterval("startCount()",1000);

          function goback(){

              window.history.go(-1);

          }

          

     </script> 

        

    </body>

    </html>


JavaScript进阶篇

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

467134 学习 · 21874 问题

查看课程

相似问题