JavaScript setTimeOut方法与setInterval方法使用

来源:互联网 发布:怪物猎人捏脸数据 编辑:程序博客网 时间:2024/05/21 11:14
在JavaScript中,我们可以在设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。

计时器类型:


一次性计时器:仅在指定的延迟时间之后触发一次。


间隔性触发计时器:每隔一定的时间间隔就触发一次。




<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>计时器</title><script type="text/javascript">  var int=setInterval(clock, 100)  function clock(){    var time=new Date();    document.getElementById("clock").value = time;  }</script></head><body>  <form>    <input type="text" id="clock" size="50"  />  </form></body></html>

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


0 0
原创粉丝点击