简体   繁体   English

以时间间隔连续重复运行JavaScript函数

[英]Run a JavaScript function continuously repeating with a time interval

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. 但是,我还没有收到结果。

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... 我想要连续重复功能的代码......

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 as @Christofer Eliasson 对于Ajax请求,您可能希望使用超时而不是间隔 ,在回调中再次启动超时,以确保在服务器占用的时间超过1时不进行堆栈调用第二个回应

Good Read 好读

  1. MDN:window.setTimeout MDN:window.setTimeout
  2. MDN:window.setInterval MDN:window.setInterval

For an Ajax-request I would use a timeout instead of an interval, and start the timeout again in the callback of the ajax-request. 对于Ajax请求,我会使用超时而不是间隔,并在ajax-request的回调中再次启动超时。

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. 如果您使用1秒的间隔并且您的服务器需要超过一秒钟来响应,您将开始以间隔堆叠呼叫,因为无论如何,间隔将每秒调用该函数。 With the timeout-in-callback approach instead, you wouldn't start the countdown until previous request has completed. 相反,使用超时回调方法,您将无法在上一个请求完成之前开始倒计时。

I'm using an IIFE to trigger the first call to the function. 我正在使用IIFE来触发对函数的第一次调用。 Then when the load has completed, I use a timeout to call the function again after one second: 然后当加载完成后,我使用超时在一秒后再次调用该函数:

(function loadContent(){
    $('#more').load('exp1.php', function () {
       setTimeout(loadContent, 1000);
    });   
})();

Just throwing it out there: 把它扔出去:

function doRequest() {
$.ajax({
  url: 'exp1.php',
  timeout: 1000,
  success: function(data) {
    $('#more').html(data);
    doRequest();
  }
});
}

How about some good ol' fashion recursion? 一些好的时尚递归怎么样?

function getStuff() {
    $('#more').load('exp1.php', function() {
        getStuff();
    });
}

getStuff();​

Demo: 演示:
http://jsfiddle.net/Zn2rh/1/ http://jsfiddle.net/Zn2rh/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM