我正在尝试使用 setTimeout 来检查表格中是否存在数据:
如果数据存在,则不获取数据。如果数据不存在,则使用 load 获取数据,然后每 x 分钟执行相同的操作。
以下是我目前的代码。出现问题的地方在于当它遇到 If 代码块时,setTimeout 无法正常工作。
我甚至不确定这是否是最好的方法。
var sTimeOut = setTimeout(function () {
$.ajax({
url: 'CheckIfDataExists/' +
new Date().getTime(),
success: function (response) {
if (response == 'True') {
$('.DataDiv')
.load('GetFreshData/' + new Date()
.getTime(), { "Id": $("#RowID").val() });
}
},
complete: function () {
clearTimeout(sTimeOut);
}
});
}, 10000);
非常感谢您提供的帮助。
更新...
setTimeout(function(){checkData()}, 5000);
function checkData(){
$.ajax({
url: 'CheckIfDataExists/' +
new Date().getTime(),
success: function (response) {
if (response == 'True') {
$('.DataDiv')
.load('GetFreshData/' + new Date()
.getTime(), { "Id": $("#RowID").val() });
} else {
$('.OutOfWindow').html('No Data Found');
setTimeout(function () { checkData() }, 5000);
}
},
complete: function () {
// clearTimeout(sTimeOut);
}
});
}