当前位置:首页 > Windows程序 > 正文

js定时器: window.clearInterval与window.setInterval的用法

2021-05-25 Windows程序

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

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

window.clearInterval()

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

语法:clearInterval(id_of_setinterval)

解释: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="开始计时" />

<input type="text" size="5" />

<input type="button" value="停止计时" />

再如:

var objTimer = window.setInterval("moveDiv()",10)是调动定时器,其中moveDiv是js的一个函数

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

引自:

js定时器: window.clearInterval与window.setInterval的用法

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70711.html