2013-02-23 2 views
0

прямо к точке У меня есть следующий код javascript и jquery, который обновляет некоторые проверенные строки и делает некоторые вещи в каждой строке datatables. вот мой код:confusing javascript setinterval, loop и jquery ajax приоритеты загрузки

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) ' s первые элементы будут одинаковыми может кто-нибудь мне помочь?
PS. я могу представить ни какой-либо код на веб
спасибо каждое тело

ответ

1

Вам нужно обернуть вызов 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); 
} 
+0

спасибо, поэтому так много помогло. хочу добавить 10 полезных голосов – Homam