2014-11-09 43 views
-1

當運行下面的代碼時,我在「back.ajax({」處得到了「Uncaught TypeError:undefined不是函數」 jQuery的文檔,這似乎是它應該做的伎倆。已經驗證,包括jQuery和瀏覽器識別「$」。CURRYEAR被定義和所有引用元素存在。如果我使用相同的代碼作爲回調到。 (),那麼它執行罰款,但我需要同步執行好象叫異步while循環只執行一次匿名函數在回調函數中給出了「undefined不是函數」

代碼:

function loadWL() { 
    var back = $("#back-results"); 
    var numYears; 
    var year; 
    var count = 0; 

    var wArr = []; 
    var lArr = []; 
    var dArr = []; 

do { 
    year = CURRYEAR - count; 

    var standingsURL = STANDINGS.replace(" ", localStorage["leagueID"]); 
    standingsURL = standingsURL.replace(",", year); 

    var yqlStand = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + standingsURL + '"') + " #maincontainertblcell"; 

    //Load into the hidden pane 
    var id; 
    var self = this; 
    back.html(""); 
    back.ajax({ 
     url: yqlStand, 
     async: false 
    }).done(function() { 
     //Parse the number of years the league has been active 
     if (count == 0) { 
      numYears = $("select > option").length; 
     } 

     //Narrow to only the elements we need 
     $(self).html($("#back-results tr .tableBody")); 

     //Now traverse the back pane and store info 
     $("#back-results > .tableBody a").closest("tr").each(function(index) { 
      id = urlToID($(this).find("a").attr('href'));          

      if (typeof wArr[id] == 'undefined') { 
       wArr[id] = 0; 
       lArr[id] = 0; 
       dArr[id] = 0; 
      } 

      wArr[id] = wArr[id] + parseInt($(':nth-child(2)', this).text().trim(),10);         
      lArr[id] = lArr[id] + parseInt($(':nth-child(3)', this).text().trim(), 10);         
      dArr[id] = dArr[id] + parseInt($(':nth-child(4)', this).text().trim(), 10);         

      if (count == numYears-1) { 
       arrayToLocal(wArr, "wins"); 
       arrayToLocal(lArr, "losses"); 
       arrayToLocal(dArr, "draws"); 
      } 
     }); 

     count++; 
     return; 
    }); 
} while (count < numYears); 

}

回答

2

back.ajax({...})應該是$.ajax({...})

ajax調用是全局的,它不綁定到任何特定的對象,所以你不要把它叫做jQuery對象,你可以在jQuery名稱空間對象上調用它。


另外,你真的應該解決您的Ajax和async: true工作,所以它不會在Ajax調用鎖住瀏覽器。這將需要將您的while循環改變爲可以對異步ajax調用進行排序的不同類型的結構。

+0

感謝您的輸入。最終完成加載以查找要加載的頁面數量,將它們放入隊列中,然後調用加載並在回調中遞歸調用其自身的函數。允許序列化加載頁面。 – PapaHenry 2014-11-09 23:32:07