以时间间隔连续重复运行 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13894768/
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
Run a JavaScript function continuously repeating with a time interval
提问by Sarvesh Godbole
This is my first question, and I would appreciate you answering soon.
这是我的第一个问题,希望您能尽快回答。
I would like code to repeat a function continuously... I have tried some code but it hasn't worked.
我希望代码能够连续重复一个函数……我尝试了一些代码,但没有奏效。
I tried this code:
我试过这个代码:
<script type="text/javascript">
$(function()
{
$('#more').load('exp1.php'); // SERIOUSLY!
});
</script>
I want to repeat this function after some interval. I have tried
setInterval()and setTimeout()
我想在一段时间后重复这个功能。我曾尝试
setInterval()和setTimeout()
But, I haven't received results.
但是,我还没有收到结果。
回答by hyankov
This will repeat the task until you clear the interval (with clearTimeout(repater))
这将重复任务,直到您清除间隔(使用 clearTimeout(repater))
var repeater;
function doWork() {
$('#more').load('exp1.php');
repeater = setTimeout(doWork, 1000);
}
doWork();
Per OP's original condition:
根据 OP 的原始条件:
I want code that repeat function continuously...
我想要连续重复功能的代码......
回答by NullPoiиteя
you can do this like
你可以这样做
var myFunction = function() {
$('#more').load('bla.php');
};
var timer = setInterval(myFunction, 1000); // call every 1000 milliseconds
or
或者
var timer = setTimeout(myFunction, 1000); // call every 1000 milliseconds
clearTimeout(timer); //To stop the function from being called forever
as @Christofer Eliasson For an Ajax-request, you would probably want to use a timeout instead of an interval, an start the timeout again in the callback, to make sure that you don't stack calls if the server is taking more than 1 second to respond
如@Christofer Eliasson 对于 Ajax 请求,您可能希望使用超时而不是时间间隔,在回调中再次启动超时,以确保在服务器占用超过 1 次时不会堆叠调用第二个回应
Good Read
好读
回答by Christofer Eliasson
For an Ajax-request I would use a timeoutinstead of an interval, and start the timeout again in the callback of the ajax-request.
对于 Ajax 请求,我将使用超时而不是间隔,并在 ajax 请求的回调中再次启动超时。
If you use an interval of say 1 second and your server takes more than one second to respond, you will start to stack calls with an interval, since the interval will call the function every second no matter what. With the timeout-in-callback approach instead, you wouldn't start the countdown until previous request has completed.
如果您使用 1 秒的间隔并且您的服务器需要超过一秒的时间来响应,您将开始以间隔堆叠调用,因为无论如何间隔都会每秒调用该函数。使用超时回调方法,您不会在前一个请求完成之前开始倒计时。
I'm using an IIFEto trigger the first call to the function. Then when the load has completed, I use a timeout to call the function again after one second:
我正在使用IIFE来触发对该函数的第一次调用。然后当加载完成时,我使用超时在一秒钟后再次调用该函数:
(function loadContent(){
$('#more').load('exp1.php', function () {
setTimeout(loadContent, 1000);
});
})();
回答by bobthyasian
Just throwing it out there:
只是把它扔在那里:
function doRequest() {
$.ajax({
url: 'exp1.php',
timeout: 1000,
success: function(data) {
$('#more').html(data);
doRequest();
}
});
}
回答by Ayman Safadi
How about some good ol' fashion recursion?
一些好的 ol' 时尚递归怎么样?
function getStuff() {
$('#more').load('exp1.php', function() {
getStuff();
});
}
getStuff();?

