最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Run a JavaScript function continuously repeating with a time interval - Stack Overflow

programmeradmin2浏览0评论

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()

But, I haven't received results.

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()

But, I haven't received results.

Share Improve this question edited Feb 19, 2017 at 3:19 ReinstateMonica3167040 7949 silver badges30 bronze badges asked Dec 15, 2012 at 18:05 Sarvesh GodboleSarvesh Godbole 891 gold badge2 silver badges6 bronze badges 3
  • 1 What was your code when you tried both set...? – bobthyasian Commented Dec 15, 2012 at 18:16
  • You should not continuously fire ajax requests. If you want live updates, use server push technologies. – Bergi Commented Dec 15, 2012 at 18:53
  • @Bergi if you want to support more browsers continuous poll is the only choice. comets and push notifications aren't supported widely yet... – Dany Khalife Commented Jul 10, 2013 at 14:27
Add a comment  | 

5 Answers 5

Reset to default 11

This will repeat the task until you clear the interval (with clearTimeout(repater))

var repeater;

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

doWork();

Per OP's original condition:

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

Good Read

  1. MDN:window.setTimeout
  2. 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.

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.

I'm using an IIFE to 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:

(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/

发布评论

评论列表(0)

  1. 暂无评论