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

逻辑应该没有错啊,为什么连秒数都没减

<!DOCTYPE html>

<html>

 <head>

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

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

 </head>

 <body>

<h2>操作成功!</h2>

<p>

  <b id = "countDown">3</b>秒后回到主页<a href="javascript:goBack();">返回</a>   

</p> 

<script  type="text/javascript">

   var seconds = document.getElementById("countDown");

  var num = 3;

function jump(){

      if(num>0){

        num--;

        seconds.innerHTML = num;

        if(num == 1){

          window.location.href="https://www.baidu.com";

        }

      }else{

        clearInterval("jump()");

  }
}

setInterval("jump()",1000);

  function goBack(){

    window.history.back();

  }   

 </script> 

</body>


</html>


提问者:qq__3259 2018-06-28 23:19

个回答

  • qq_丿iTTF鹰_0
    2018-06-29 11:49:18
    已采纳

    <!DOCTYPE html>
    
    <html>
    
    <head>
    
        <title>浏览器对象</title>
    
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
    </head>
    
    <body>
    
    <h2>操作成功!</h2>
    
    <p>
    
        <b id = "countDown">3</b>秒后回到主页<a href="javascript:goBack();">返回</a>
    
    </p>
    
    <script  type="text/javascript">
    
        var num = document.getElementById("countDown").innerHTML;
    
    
    
        function jump(){
            --num;
            document.getElementById("countDown").innerHTML=num;
            if(num>0){
    
                if(num == 1){
    
                    window.location.assign("https://www.baidu.com");
    
                }
    
            }else{
    
                clearInterval("jump()");
            }
        }
    
        setInterval("jump()",1000);
    
        function goBack(){
    
            window.history.back();
    
        }
    
    </script>
    
    </body>
    
    
    
    
    </html>

    问题自减是--num;

    这里 var seconds = document.getElementById("countDown");

      var num = 3;

    function jump(){

          if(num>0){

            num--;

            seconds.innerHTML = num;

            if(num == 1){

              window.location.href="https://www.baidu.com";

            }

          }else{

            clearInterval("jump()");

      }
    }

    你获取了值 但是 你没处理  返回的 所以返回的 还是3

  • qq_丿iTTF鹰_0
    2018-07-05 14:55:44

    这句话是获取id而已