window.clearInterval与window.setInterval的用法

转载自:http://www.cnblogs.com/liences/archive/2011/11/25/2262883.html

window.clearInterval与window.setInterval的用法

 

window.setInterval()javascript

功能:按照指定的周期(以毫秒计)来调用函数或计算表达式。html

语法:setInterval(code,millisec)java

 

解释:code:在定时时间到时要执行的JavaScript代码串。函数

millisec:设定的定时时间,用毫秒数表示。post

返回值:定时器的ID值,可用于clearInterval()方法中止指定的定时器。spa

注:setInterval()方法会不停地调用函数,直到用clearInterval()终止定时或窗口被关闭。code

window.clearInterval()htm

功能:取消由setInterval()方法设置的定时器。blog

语法:clearInterval(id_of_setinterval)ip

解释:id_of_setinterval:由setInterval()返回的ID值。该值标识了一个setInterval定时器。

也就是:window.setInterval()返回的就是window.clearInterval的参数

例子:

<script type="text/javascript">
var count = 0;
var timeID;
function timeCount()
{
  document.getElementByIdx('timetxt').value = count;
  count++;
}
function beginCount()
{
  timeID = setInterval("timeCount()",1000);
}
function stopCount()
{
  clearInterval(timeID);
}
</script>
<input type="button" value="开始计时" onclick="beginCount()" />
<input type="text" id="timetxt" size="5" />
<input type="button" value="中止计时" onclick="stopCount()" />
 再如:
var objTimer = window.setInterval("moveDiv()",10)是调动定时器,其中moveDiv是js的一个函数

if(objTimer) window.clearInterval(objTimer)是中止定时器

相关文章
相关标签/搜索