混淆了javascript的setinterval、循环和jquery ajax负载优先级

confusing javascript setinterval, loop and jquery ajax load priorities

本文关键字:jquery ajax 负载 优先级 循环 javascript setinterval      更新时间:2023-09-26

直接指向点我有下面的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);
}