gpt4 book ai didi

javascript - 页面刷新时加载数据 - dataTable

转载 作者:行者123 更新时间:2023-12-02 16:59:38 24 4
gpt4 key购买 nike

我的数据表代码全部按预期工作,因为它根据时间间隔使用数据刷新表。我唯一想要修复的是,当有人刷新整个页面时,在触发时间间隔设置之前还没有加载数据。有没有办法,只有当有人刷新整个页面时,我才能立即加载数据,然后按照设定的间隔开始加载。谢谢

<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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com