【发布时间】:2019-05-18 20:07:06
【问题描述】:
请提供帮助。
我正在尝试访问在 JQuery DataTable() 函数之外声明的对象变量。我已经为 Ajax 对象提供了设置,其中一个回调函数在请求成功时完成执行。由于不推荐使用 async: false,因此我决定使用 setTimeout() 访问从外部回调函数初始化的变量。请查看我的代码以澄清我的问题。
var odata = {
ids: [],
dates: []
};
var table = $("#schedule");
table.DataTable({
ajax: {
url: "/api/MaintenanceSchedule",
dataSrc: "",
complete: function (data, status) {
if (status === "success") {
//some codes here
}
$("span.machineIds").each(function (index) {
machineIds[index] = $(this).attr("data-machine-id");//here the array output all elements if you check with console.log()
});
$("span.lastMaintained").each(function (index) {
lastMaintained[index] = $(this).attr("data-last-maintained");
});
//the odata properties below have assigned values as seen from the debugger window
odata = {
ids: machineIds,
dates: lastMaintained
};
}
//some other codes ...
//outside DataTable object
var checkMachineState = function (odata, interval) {
// some codes...
}
const INTERVAL = 45000;
setTimeout(checkMachineState(odata,INTERVAL),5000);//odata properties are still not initialized as seen from the debugger
调试器显示如下
odata:对象 日期: [] ID:数组(0) 长度:0 原型:数组(0) 原型:对象
【问题讨论】:
标签: javascript jquery ajax datatables