Javascript 每 60 秒调用一次函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3138756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a function every 60 seconds
提问by Richard Knop
Using setTimeout()it is possible to launch a function at a specified time:
使用setTimeout()它可以在指定的时间启动一个函数:
setTimeout(function, 60000);
But what if I would like to launch the function multiple times? Every time a time interval passes, I would like to execute the function (every 60 seconds, let's say).
但是如果我想多次启动该功能怎么办?每次经过一个时间间隔时,我都想执行该函数(假设每 60 秒一次)。
回答by jAndy
If you don't care if the code within the timermay take longer than your interval, use setInterval():
如果您不关心其中的代码是否timer可能需要比您的时间间隔更长的时间,请使用setInterval():
setInterval(function, delay)
That fires the function passed in as first parameter over and over.
这会一遍又一遍地触发作为第一个参数传入的函数。
A better approach is, to use setTimeoutalong with a self-executing anonymousfunction:
更好的方法是,setTimeout与self-executing anonymous函数一起使用:
(function(){
// do some stuff
setTimeout(arguments.callee, 60000);
})();
that guarantees, that the next call is not made before your code was executed. I used arguments.calleein this example as function reference. It's a better way to give the function a name and call that within setTimeoutbecause arguments.calleeis deprecated in ecmascript 5.
这保证了在您的代码执行之前不会进行下一次调用。我arguments.callee在这个例子中用作函数参考。这是给函数命名并在其中调用的更好方法,setTimeout因为arguments.callee在 ecmascript 5 中已弃用。
回答by BlueBird
use the
使用
setInterval(function, 60000);
EDIT : (In case if you want to stop the clock after it is started)
编辑:(如果您想在启动后停止时钟)
Script section
脚本部分
<script>
var int=self.setInterval(function, 60000);
</script>
and HTML Code
和 HTML 代码
<!-- Stop Button -->
<a href="#" onclick="window.clearInterval(int);return false;">Stop</a>
回答by Om Shankar
A better use of jAndy's answerto implement a polling function that polls every intervalseconds, and ends after timeoutseconds.
更好地利用jAndy的答案来实现轮询功能,该功能每秒轮询一次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);
UPDATE
更新
As per the comment, updating it for the ability of the passed function to stop the polling:
根据评论,更新它以获取传递函数停止轮询的能力:
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.
}
}
回答by Optimum Creative
In jQuery you can do like this.
在 jQuery 中,您可以这样做。
function random_no(){
var ran=Math.random();
jQuery('#random_no_container').html(ran);
}
window.setInterval(function(){
/// call your function here
random_no();
}, 6000); // Change Interval here to test. For eg: 5000 for 5 sec
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="random_no_container">
Hello. Here you can see random numbers after every 6 sec
</div>
回答by Jamiec
setInterval(fn,time)
is the method you're after.
是你所追求的方法。
回答by John Henckel
You can simply call setTimeout at the end of the function. This will add it again to the event queue. You can use any kind of logic to vary the delay values. For example,
您可以简单地在函数末尾调用 setTimeout。这将再次将其添加到事件队列中。您可以使用任何类型的逻辑来改变延迟值。例如,
function multiStep() {
// do some work here
blah_blah_whatever();
var newtime = 60000;
if (!requestStop) {
setTimeout(multiStep, newtime);
}
}
回答by christian louboutin sale
回答by srghma
// example:
// checkEach(1000, () => {
// if(!canIDoWorkNow()) {
// return true // try again after 1 second
// }
//
// doWork()
// })
export function checkEach(milliseconds, fn) {
const timer = setInterval(
() => {
try {
const retry = fn()
if (retry !== true) {
clearInterval(timer)
}
} catch (e) {
clearInterval(timer)
throw e
}
},
milliseconds
)
}
回答by Vijay sadhu
here we console natural number 0 to ......n (next number print in console every 60 sec.) , using setInterval()
在这里,我们使用 setInterval() 将自然数 0 控制为 ......n(每 60 秒在控制台中打印下一个数字)
var count = 0;
function abc(){
count ++;
console.log(count);
}
setInterval(abc,60*1000);
回答by Gaurav Tripathi
There are 2 ways to call-
有两种调用方式——
setInterval(function (){ functionName();}, 60000);setInterval(functionName, 60000);
setInterval(function (){ functionName();}, 60000);setInterval(functionName, 60000);
above function will call on every 60 seconds.
上述函数将每 60 秒调用一次。

