可以解释一下为什么第一次跳转到负数时才跳转?

<!DOCTYPE html>

<html>

 <head>

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

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

 </head>

 <body>

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

 

  <h1>操作成功</h1>

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

  <span>秒后回到主页</span>

  <a href="javascript:back();">返回</a>

 

  <script type="text/javascript">  

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

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

function du(){

    

    num=num-5;

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

    if(num==0)

    {

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

    }

}

    setInterval("du()",1000);

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

    function back()

    {

       window.history.back();

   }

 </script> 

</body>

</html>


慕粉1716012316
浏览 1210回答 1
1回答

习惯受伤

用下面这段代码代替:<script type="text/javascript"> //获取显示秒数的元素,通过定时器来更改秒数。 var num = document.getElementById("dd").innerHTML; var handler = 0; function du() { num = parseInt(num) - 1; //这里,parseInt(num) 确保整数相减 if(num == 0) { location.assign("http://www.imooc.com"); clearInterval(handler); //清除定时器,不执行下边的显示秒数代码 return; } document.getElementById("dd").innerHTML = num; } handler = setInterval("du()", 1000); </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript