2014-11-09 2 views
-1

아래 코드를 실행하면 "uncaught TypeError : undefined is not function"이라는 줄이 "back.ajax ({". jQuery 문서 및이 트릭을해야합니다 것 같아요. 또한 jQuery 포함되어 있고 브라우저가 "$"인식합니다 .CURRYEAR 정의 된 및 모든 요소가 참조 된.이 콜백 같은 코드를 사용하는 경우 .load() 다음 그것을 잘 실행,하지만 난 비동기 적으로 호출하는 경우 while 루프는 한 번만 실행으로 동 기적으로 실행해야익명 함수는 콜백에서 "정의되지 않음이 함수가 아닙니다"라고 알려줍니다.

코드 :.

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 네임 스페이스 객체에서 호출합니다. 또한


, 당신은 정말 그렇게는 아약스 통화 중에 브라우저를 고정하지 않습니다 async: true와 함께 작동하도록 아약스를 해결해야한다. while 루프를 비동기 아약스 호출을 시퀀싱 할 수있는 다른 유형의 구조로 변경해야합니다.

+0

입력 해 주셔서 감사합니다. 로드 할 페이지 수를 찾기 위해로드를 끝내고 큐에 넣은 다음 콜백에서로드되고 재귀 적으로 자신을 호출하는 함수를 호출합니다. 페이지의 직렬로드를 허용합니다. – PapaHenry

관련 문제