我有一些数据在我的数据库。我用PHP和MySQL选择数据,并将其放入JSON中。我加载JSON在我的其他网站到我的列表视图。它的工作原理。但是我想要的是当JSON数据改变时,我的listview应该自动重新加载。
jQuery代码$(document).ready(function(){
var url="url";
$.getJSON(url,function(json){
$.each(json.post,function(i,post){
$("ul:jqmData(role='listview')").append("<li>"+post.naam+"</li>").listview('refresh');
});
});
});
我希望有人能帮助我。
谢谢。
假设您希望每隔10秒刷新一次,并且您不知道服务器是否更改了数据,那么您应该每隔10秒提取一次数据,然后清空listview并刷新数据:
var autorefresh = null;
function autoUpdate(){
if(autorefresh)
clearTimeout(autorefresh);
autorefresh=null;
$.getJSON(url,function(json){
var $list = $("ul:jqmData(role='listview')");
$list.empty();
$.each(json.post,function(i,post){
$list.append("<li>"+post.naam+"</li>");
});
$list.listview('refresh');
autorefresh=setTimeout(autoUpdate,10000);
});
}
$(autoUpdate);
你可能想要保护getJson,以防出现错误,我没有在这里做