每60秒调用一个函数使用setTimeout()可以在指定的时间启动一个函数:setTimeout(function, 60000);但是如果我想多次启动这个函数呢?每隔一段时间间隔,我都想执行这个函数(比方说,每60秒一次)。
3 回答
不负相思意
TA贡献1777条经验 获得超10个赞
timer
setInterval()
:
setInterval(function, delay)
setTimeout
self-executing anonymous
(function(){ // do some stuff setTimeout(arguments.callee, 60000);})();
arguments.callee
setTimeout
arguments.callee
慕雪6442864
TA贡献1812条经验 获得超5个赞
setInterval(function, 60000);
<script>var int=self.setInterval(function, 60000);</script>
<!-- Stop Button --><a href="#" onclick="window.clearInterval(int);return false;">Stop</a>
潇湘沐
TA贡献1816条经验 获得超6个赞
interval
timeout
function pollFunc(fn, timeout, interval) { var startTime = (new Date()).getTime(); interval = interval || 1000; (function p() { fn(); if (((new Date).getTime() - startTime ) <= timeout) { setTimeout(p, interval); } })();}pollFunc(sendHeartBeat, 60000, 1000);
更新
function pollFunc(fn, timeout, interval) { var startTime = (new Date()).getTime(); interval = interval || 1000, canPoll = true; (function p() { canPoll = ((new Date).getTime() - startTime ) <= timeout; if (!fn() && canPoll) { // ensures the function exucutes setTimeout(p, interval); } })();}pollFunc(sendHeartBeat, 60000, 1000);function sendHeartBeat(params) { ... ... if (receivedData) { // no need to execute further return true; // or false, change the IIFE inside condition accordingly. }}
添加回答
举报
0/150
提交
取消