我的数据表代码都按预期工作,因为它可以根据时间间隔刷新数据表。我要解决的唯一问题是,当有人刷新整个页面时,直到触发时间间隔设置为止,尚未加载任何数据。有什么方法可以让我仅在有人刷新整个页面时立即加载数据,然后设置的时间间隔才开始。谢谢
<head>
<script type="text/javascript" charset="utf-8">
<!-- ------------------- Function to display maintenance messages ---------------------- -->
$(document).ready(function() {
var maintenanceTable = $('#maint-table').DataTable({
"jQueryUI": true,
"columns": [
{ "data": "msg" }
],
"language": {
"emptyTable": "No Maintenances Available in Table"
}
});
setInterval (function(){
$.getJSON("include/maintenance.php", function (pcheckmaint){
maintenanceTable.clear().draw();
maintenanceTable.rows.add(pcheckmaint.maint).draw();
maintenanceTable.columns.adjust().draw();
});
}, 120000);
});
</script>
<script type="text/javascript" charset="utf-8">
<!-- ------------------- Extract all Alerts ---------------------- -->
$(document).ready(function (){
var alertTable = $('#alert-table').DataTable({
"jQueryUI": true,
"columns": [
{ "data": "source", "visible": false },
{ "data": "host" },
{ "data": "description" },
{ "data": "priority" },
{ "data": "acknowledged"}
],
});
setInterval (function(){
$.getJSON("data/json_data.txt", function (pcheckdata){
alertTable.clear().draw();
alertTable.rows.add(pcheckdata.alert).draw();
alertTable.columns.adjust().draw();
});
}, 10000);
});
function ackbutton() {
//e.preventDefault();
var $this = $(this);
var getvalues = $('#evtid').val();
alert(getvalues);
}
</script>
</head>
最佳答案
从setInterval调用中删除该函数,并将其声明为命名函数。然后调用该函数,然后再次调用setInterval到该函数。这样,它将立即调用一次,然后重复执行。
getData();
getMaintenance();
setInterval(getData, 10000);
setInterval(getMaintenance, 120000);
function getData()
{
$.getJSON("data/json_data.txt", function (pcheckdata)
{
alertTable.clear().draw();
alertTable.rows.add(pcheckdata.alert).draw();
alertTable.columns.adjust().draw();
});
}
function getMaintenance()
{
$.getJSON("include/maintenance.php", function (pcheckmaint)
{
maintenanceTable.clear().draw();
maintenanceTable.rows.add(pcheckmaint.maint).draw();
maintenanceTable.columns.adjust().draw();
});
}
关于javascript - 在页面刷新上加载数据-dataTable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25865645/