我的数据表代码全部按预期工作,因为它使用基于时间间隔的数据刷新表。我试图解决的唯一问题是当有人刷新整个页面时,在触发时间间隔设置之前,没有数据被加载。有没有办法只有当有人刷新整个页面时我才能立即加载数据,然后设置的间隔开始。感谢
<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>
答案 0 :(得分:2)
从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();
});
}