混淆了javascript的setinterval、循环和jquery ajax负载优先级
confusing javascript setinterval, loop and jquery ajax load priorities
直接指向点我有下面的javascript和jquery代码,它们更新了一些选中的行,并对每个datatables行执行了一些操作。这是我的代码:
function checkUpdate(){
setInterval(function(){
var listLength = updateList.length;
if(listLength > 0){
for(var r=0; r<listLength; r++){
// console.log(r)
var clID = updateList[r];
// console.log(clID)
var rRow = $('#dataTable tbody tr').find('td[data-clientid="'+clID+'"]').parent('tr');
// console.log(rRow)
var rRowIndex = rRow.index();
// console.log(rRowIndex)
var rRowDataIndex = oTable.fnGetPosition(rRow[0]);
console.log(rRowDataIndex)
$.ajax({
url: '/cgi-bin/if-Clients-list.jpl',
data: 'session=' + recievedSession + '&clientid=' + clID + '&outputformat=json',
dataType: 'json',
success: function(rowData){
// console.log(rowData)
var newRow = [];
var newOrderedRow = [];
console.log(rRowDataIndex)
newRow.push(rRowDataIndex+1, "");
for (var title in rowData[0]){
newRow.push(rowData[0][title]);
}
console.log(newRow)
},
});
};
}
},2000)
};
问题是:
在$.ajax()
调用之后,rRowDataIndex
变量不更新,或者它更新了,但范围和优先级存在问题,我无法理解如果我检查2行或更多行,则console.log(newRow)
的所有第一个元素都将相同有人能帮我吗
PS我不能在网上展示任何代码
感谢每一个人
您需要将AJAX调用封装在一个闭包中,以便每次通过循环捕获rRowDataIndex
的值。
function checkUpdate() {
setInterval(function () {
var listLength = updateList.length;
if (listLength > 0) {
for (var r = 0; r < listLength; r++) {
// console.log(r)
var clID = updateList[r];
// console.log(clID)
var rRow = $('#dataTable tbody tr').find('td[data-clientid="' + clID + '"]').parent('tr');
// console.log(rRow)
var rRowIndex = rRow.index();
// console.log(rRowIndex)
var rRowDataIndex = oTable.fnGetPosition(rRow[0]);
console.log(rRowDataIndex)
(function (rRowDataIndex) {
$.ajax({
url: '/cgi-bin/if-Clients-list.jpl',
data: 'session=' + recievedSession + '&clientid=' + clID + '&outputformat=json',
dataType: 'json',
success: function (rowData) {
// console.log(rowData)
var newRow = [];
var newOrderedRow = [];
console.log(rRowDataIndex)
newRow.push(rRowDataIndex + 1, "");
for (var title in rowData[0]) {
newRow.push(rowData[0][title]);
}
console.log(newRow)
},
});
})(rRowDataIndex);
};
}
}, 2000);
}
相关文章:
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 将curl查询转换为jQuery.ajax()
- jQuery AJAX write to XML
- jQuery Ajax GET请求工作不正常
- jquery ajax https调用给出ERR_INSECURE_RESPONSE
- VB NET JQUERY AJAX上出现错误500
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- Jquery/Ajax.serialize()未完全工作
- 页面加载之前的jQuery Ajax加载程序
- jquery/ajax无限滚动事件
- 使用Jquery$.ajax将json数据传递给servlet(doPost)
- 如何将给定的curl命令复制为jquery ajax请求
- 使用 JQUERY AJAX (Spring MVC) 从控制器中删除
- Twitter API在jQuery AJAX中设置授权头
- AngularJS JQuery Ajax表单提交等效
- jQuery Ajax.ajaxSuccess()事件未激发
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- jQuery AJAX总是出错
- jQuery Ajax数组序列化错误
- jQuery AJAX获取Vimeo缩略图