【发布时间】:2018-05-22 10:34:56
【问题描述】:
我正在尝试绑定数据库中的数据。发生的事情是它每 5 秒绑定一次。现在它的绑定正确,但它不会清除早期的数据。它一直在起球。所以如果有 3 行 .. 它只需要显示 3 行。现在发生的事情是它每 5 秒添加 3 行,并且每 5 秒保持 6-9-12 起球。 我需要清除数据,然后每 5 秒加载一次。
<script type="text/javascript">
$(document).ready(function () {
Get();
setInterval(function () {
Get() // this will run after every 5 seconds
}, 5000);
});
function Get() {
$.ajax({
type: "POST",
url: "../adminpage/noti.ashx",
data: {},
dataType: "json",
success: function (response) {
$.each(response, function (index, itemData) {
var tr = "<tr>" +
"<td>" + itemData.msg + "</td>" +
"<td>" + itemData.dt + "</td>" +
"</tr>";
$("#testTable").find("tbody").append(tr);
});
BindTable();
}
});
}
function BindTable() {
try {
$('#testTable thead th').each(function (i) {
var title = $('#testTable thead th').eq($(this).index()).text();
if (title != "") {
$(this).html('<div style="width:40%;text-align:center;white-space:nowrap">' + title + '</div>');
}
});
}
catch (e) { }
}
</script>
<table id="testTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th class="m-list-timeline__text" style="width:70%">msg</th>
<th class="m-list-timeline__time" style="width:30%">dt</th>
</tr>
</thead>
<tbody></tbody>
</table>
【问题讨论】:
标签: javascript asp.net ajax datatables