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.
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.
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);
使用setTimeout
的好处是,如果连接挂起一段时间,您将不会收到大量待处理的请求,因为只有在前一个完成后才会发送新的请求。
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();
$(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
}
如果你使用 jQuery,你可以使用load()方法
setInterval(function(){
$('#some-kinda-container').load('/some/kinda/url.html #bit-you-need');
}, 30000);