阮一峰的IT笔记:首页 -> 分类 -> Javascript 查看所有文章:按分类 | 按月份

JavaScript 函数:setTimeout( ) 和setInterval( )

setTimeout( )是定时程序,用来确定多少毫秒以后干什么。setTimeout( )返回一个句柄,以供clearTimeout( )撤销这个操作。

setInterval( )表示间隔多少毫秒反复执行某操作,setInterval( )返回一个句柄,以供clearInterval( )撤销这个操作。

举一个例子:

 <div id="liujincai"></div>
<input type="button" name="start" value="start" onclick='startShow();'>
<input type="button" name="stop" value="stop" onclick="stop();">
<script language="javascript">
   var intvalue=1;
   var timer2=null;
   function startShow()
   {
      liujincai.innerHTML=liujincai.innerHTML + "&nbsp;" + (intvalue ++).toString();
      timer2=window.setTimeout("startShow()",2000);
   }
   function stop()
   {
      window.clearTimeout(timer2);
   }
</script>

或者:

 <div id="liujincai"></div>
<input type="button" name="start" value="start" onclick='timer2=window.setInterval("startShow()",2000);//startShow();'>
<input type="button" name="stop" value="stop" onclick="stop();">
<script language="javascript">
   var intvalue=1;
   var timer2=null;
   function startShow()
   {
      liujincai.innerHTML=liujincai.innerHTML + "&nbsp;" + (intvalue ++).toString();
   }
   function stop()
   {
      window.clearInterval(timer2);
   }
</script>

« TCP/IP协议基本知识 | 首页 | 操作系统基本知识 »

About

This page contains a single entry from the blog posted on 2007年02月28日 20:10.

The previous post in this blog was TCP/IP协议基本知识.

The next post in this blog is 操作系统基本知识.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment