How to load a part of page every 30 seconds [duplicate]

∥☆過路亽.° 提交于 2019-12-22 08:05:06

问题


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.


回答1:


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.




回答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();



回答3:


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.




回答4:


$(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
}



回答5:


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


来源:https://stackoverflow.com/questions/11342199/how-to-load-a-part-of-page-every-30-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!