4

Possible Duplicate:
jQuery Ajax request every 30 seconds

I know we can load a part of page on some event. I also know we can load whole web page every specified time, but I wanted to know how to load a part of page every 30 seconds.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sapna Agrawal
  • 117
  • 2
  • 8
  • 1
    If you use AJAX you can can call AJAX method using `setInterval()` function. https://developer.mozilla.org/En/Window.setInterval – antyrat Jul 05 '12 at 10:15
  • @antyrat using `setInterval` with AJAX is a **very** bad idea. – Florian Margaine Jul 05 '12 at 10:18
  • @FlorianMargaine so what's the good idea? – Nadav S. Jul 05 '12 at 10:18
  • I dont want to use setinterval, as it unnecessary hit the server, even if there is no update. IS there any way so that once data is updated on server, it loads on my page. – Sapna Agrawal Jul 05 '12 at 10:20
  • You could look into long poll ajax calls, which are ajax calls that wait for a long time, then timeout, then reconnect for a long time again, then get data, then reconnect etc. – Question Mark Jul 05 '12 at 10:34
  • @SapnaAgrawal: You need to ask a new question for this. It is too different from your initial question. What you are looking for are WebSockets or COMET-style AJAX long polling. – ThiefMaster Jul 05 '12 at 10:52

5 Answers5

12
function refreshPage() {
    $.ajax({
        url: 'ajax/test.html',
        dataType: 'html',
        success: function(data) {
            $('.result').html(data);
        },
        complete: function() {
            window.setTimeout(refreshPage, 30000);
        }
    });
}

window.setTimeout(refreshPage, 30000);

Using setTimeout has the advantage that if the connection hangs for some time you will not get tons of pending requests since a new one will only be sent after the previous one finished.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2
function load_content(){

    setTimeout(function(){

        $.ajax({
            url: 'ajax/example.html',
            dataType: 'html',
            success: function(data) {
                $('.result').html(data);
                load_content();
            }
        });dataType: 'html',

    },30000);

}

load_content();
472084
  • 17,666
  • 10
  • 63
  • 81
0

jQuery has already a build in functionality to replace a element's content by a remote file, called load(). With load() you can use this oneliner:

window.setTimeout($('#refresh').load('/remote/content.html'), 30000);

#refresh is the id of the element to refresh, /remote/content.html is the remote content.

powtac
  • 40,542
  • 28
  • 115
  • 170
-1
$(function() {
   setInterval(function() {
     getData();  // call to function
   }, 30000 );  // 30 seconds
});


// define your function here
function getData() {
   var url ="/mypage.php?type=load_data";
   var httpobj = $.ajax({url:url,async:false});  // send request
   var response = httpobj.responseText.trim(); //get response
   $('#myDiv').html(response);  // display data
}
Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35
-2

If you are using jQuery you can use the load() method

setInterval(function(){

    $('#some-kinda-container').load('/some/kinda/url.html #bit-you-need');

}, 30000);
Question Mark
  • 3,557
  • 1
  • 25
  • 30
  • See @Jleagle's answers' comments to see why it's a **very** bad idea to use `setInterval`. – Florian Margaine Jul 05 '12 at 10:21
  • if your ajax calls aren't completing in 30 seconds you have bigger things to worry about than setInterval – Question Mark Jul 05 '12 at 10:23
  • It's not about 30 seconds the first time, it's about remaining constant. If you need 2s to get the response, you'll overlap your requests in 5 minutes or so (didn't calculate, but you get the point) – Florian Margaine Jul 05 '12 at 10:25
  • There will only be overlap if the load() takes longer than 30 seconds surely? – Question Mark Jul 05 '12 at 10:32
  • No, there will be overlap after multiple requests, each one taking 2 seconds. 15 requests later, two requests will overlap each other. – Florian Margaine Jul 05 '12 at 10:39
  • Because `setInterval` doesn't wait for your request to finish. It will be run every 30 seconds, no matter what. – Florian Margaine Jul 05 '12 at 10:46
  • If you use SetTimeout then it will be 30 seconds plus 2 seconds fetch time so will definitely be out of sync and not what the op asked for, i don't understand your point i think, as setInterval will give you an ajax request after every x seconds no matter what the ajax delay, assuming the ajax delay is no longer than the interval there will be no overlap: http://jsfiddle.net/tDhGf/1/ – Question Mark Jul 05 '12 at 12:04