我想刷新一个填充JSON数据的表,例如每5秒钟。
应该在没有完整站点刷新的情况下刷新表。应添加新项目,应删除丢失的项目。如何使用AJAX刷新它?
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<table face="comic sans ms" border="1" id="usertable"></table>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON('./url.php', function(data) {
var cars = data.rec.vehicles.vehicles;
$.each(cars, function(key, data){
console.log(data.carName);
var vint=data.vin;
var coopers="";
var tanken="";
var drive="MAN";
var fuelfarbe="#01DF3A";
var prozent="%";
var v=vint.substring(3,7);
if(v=="SX31"){coopers=" S";}
if(v=="SW71"){coopers=" SD";}
if(data.auto=="Y"){drive="AUT";}
if(data.fuelState<=25){tanken="20 Freiminuten für Vollbetankung"; fuelfarbe="#FF4000";}
$('#usertable').append(
function() {
return "<tr>"+
"<td>"+data.carName+"</td>"+
"<td>"+data.model+coopers+"</td>"+
"<td>"+drive+"</td>"+
"<td>"+data.fuelState+prozent+"</td>"+
//"<td>"+data.address+"</td>"+
"<td>"+data.licensePlate+"</td>"+
"<td>"+tanken+"</td>"
"<tr>";
}
);
})
});
});
</script>
</body>
</html>
答案 0 :(得分:0)
将getJSON
保留在函数中
function dummy()
{
$(function() {
$.getJSON(
"url",
function(json){ });
});
}
和
给
setInterval( dummy, 5000 );