Javascript ajax函数自动重新加载
您好,我正在从一个php文件中获取json格式的数据,一切正常。但是,我希望该函数每隔3000毫秒重新加载一次,从而更新数据 我如何做到这一点Javascript ajax函数自动重新加载,javascript,jquery,ajax,reloading,Javascript,Jquery,Ajax,Reloading,您好,我正在从一个php文件中获取json格式的数据,一切正常。但是,我希望该函数每隔3000毫秒重新加载一次,从而更新数据 我如何做到这一点 function loadFbill(hsid) { // Display a loading icon in our display element $('.list-item-display').html('<span><img src="img/progress.gif" /></span>');
function loadFbill(hsid)
{
// Display a loading icon in our display element
$('.list-item-display').html('<span><img src="img/progress.gif" /></span>');
// Request the JSON and process it
$.ajax({
type:'GET',
url:""+xhr_path+"js_on.php",
data:"hid="+hsid+"&subscribers=paid",
success:function(feed) {
// Create an empty array to store images
var thumbs = [];
// Loop through the items
for(var i=0, l=feed.response.length; i < l && i < 6; ++i)
{
// Manipulate the image to get thumb and medium sizes
var payee = feed.response[i].result_paid;
var transaction_m = feed.response[i].result_payment_details;
var ptime = feed.response[i].result_time;
var plan = feed.response[i].result_plan;
var payee_num = feed.response[i].result_num;
var localhs = feed.response[i].result_hs;
var transaction_date = feed.response[i].result_payment_date;
var diff_date = feed.response[i].result_time_diff;
// Add the new element to the array
thumbs.push("<div class=list-item><div class=list-datetime><div class=time>"+ptime+"</div></div><div class=list-info><img src=img/pesa/ps_"+transaction_m+".jpg height=28 width=28 class=img-circle img-thumbnail/></div><div class=list-text><a href=# class=list-text-name> "+payee+" </a><p>"+payee_num+" <span class=icon-calendar></span> "+transaction_date+" <span class=icon-calendar-empty></span>"+diff_date+"<span class=icon-globe></span> "+localhs+" </p></div><div class=list-controls> "+plan+"</div></div>");
}
// Display the thumbnails on the page
$('.list-item-display').html(""+thumbs.join('')+"");
// A function to add a lightbox effect
addLB();
},
dataType:'json'
});
}
函数加载填充(hsid)
{
//在显示元素中显示加载图标
$('.list item display').html('');
//请求JSON并对其进行处理
$.ajax({
类型:'GET',
url:“+xhr_路径+”js_on.php”,
数据:“hid=“+hsid+”&订阅者=付费”,
成功:功能(提要){
//创建一个空数组来存储图像
var thumbs=[];
//循环浏览项目
对于(变量i=0,l=feed.response.length;i“+plan+”);
}
//在页面上显示缩略图
$('.list item display').html(“+thumbs.join”(“”)+”);
//用于添加灯箱效果的函数
addLB();
},
数据类型:'json'
});
}
您只需将setTimeout
添加到ajax请求回调函数中:
function loadFbill(hsid) {
// Display a loading icon in our display element
$('.list-item-display').html('<span><img src="img/progress.gif" /></span>');
// Request the JSON and process it
$.ajax({
type: 'GET',
url: "" + xhr_path + "js_on.php",
data: "hid=" + hsid + "&subscribers=paid",
success: function (feed) {
// ... all your code untouched
setTimeout(loadFbill, 3000); // <-- and reload again
},
dataType: 'json'
});
}
函数加载填充(hsid){
//在显示元素中显示加载图标
$('.list item display').html('');
//请求JSON并对其进行处理
$.ajax({
键入:“GET”,
url:“+xhr_路径+”js_on.php”,
数据:“hid=“+hsid+”&订阅者=付费”,
成功:功能(提要){
//…你所有的代码都没动过
setTimeout(loadFbill,3000);//如果在ajax调用期间发生错误怎么办?