2013-02-23 75 views
0

開門見山 我有以下的javascript和jQuery代碼更新一些檢查rowsand做一些東西在每個數據錶行。這裏是我的代碼:混亂的JavaScript的setInterval,循環和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)「第一個要素是相同的 誰能幫助我?
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 2013-02-24 06:14:00