JS 页面计时器示例代码

复制代码 代码如下:

  <html>

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

  <head>

  <script type="text/javascript">

  var c=0

  var t

  function timedCount()

  {

  document.getElementById('txt').value=c

  c=c+1

  t=setTimeout("timedCount()",1000)

  }

  function stopCount()

  {

  clearTimeout(t)

  }

  </script>

  </head>

  <body>

  <form>

  <input type="button" value="开始计时!" onClick="timedCount()">

  <input type="text" id="txt">

  <input type="button" value="停止计时!" onClick="stopCount()">

  </form>

  <p>请点击上面的“开始计时”按钮来启动计时器。输入框会一直进行计时,从 0 开始。点击“停止计时”按钮可以终止计时。</p>

  <p>timedCount(),1000ms自己调自己。</p>

  </body>

  </html>