0
FOR
루프에 표시되는 아이템 ID gameIds[i]_container
의 목록이 있습니다. 켜짐 문제없이 표시됩니다.Javascript FOR 루프 콜백
해당 항목에 콘솔의 항목 gameIds[i]
을 기록하도록합니다. 나는 이것이 콜백으로 수행되어야한다고 생각하지만, 현재 항목들이 클릭 될 때 아무 일도 일어나지 않는다.
도와주세요!
코드 :
//CLICKING ON GAMES
//Active Game - bring user to game
//Newly Invited Game - ask user to accept invite, then either bring to game or delete from players list
//Rematch Game - recreate previous game
//Function
function gameAccess(i) {
//REMATCH GAME
if (currentRound[i] == roundWinners[i].length && currentRound[i] != 0 && currentRound[i] == numberOfRounds[i]){
console.log("You clicked " + gameIds[i]);
}
//NEWLY INVITED GAME
if (currentRound[i] == 0 && currentUserId != creator[i]) {
console.log("You clicked " + gameIds[i]);
}
//ACTIVE GAME
else{
console.log("You clicked " + gameIds[i]);
}
}
//Callback
function gameAccessCallback(i){
return function(){
gameAccess(i);
};
}
//Iterate Through
for (i = 0; i < numberOf; i++) {
document.getElementById(gameIds[i] + "_container ").click(gameAccessCallback(i));
};
귀하의 질문은 [I] _container''gameIds 언급,하지만 자바 스크립트 유효하지 않습니다
이보십시오. – jfriend00
비슷한 질문 : http://stackoverflow.com/questions/8623088/undefined-indexes-in-array-after-asynchronous-requests/8623120#8623120 및 http://stackoverflow.com/questions/18880895/why-when- i-add-a-bunch-of-event-listen-in-a-loop-does-every-element-trigger/18880964 # 18880964 및 http://stackoverflow.com/questions/21487187/why-is-the- 루프 할당 - 마지막 - 인덱스 요소 - 투 - 21487254 # 21487254 및 http://stackoverflow.com/questions/22273839/proper-code-for-a-for-loop-in- javascript/22273973 # 22273973 – jfriend00
나는 이것이 중복이라고 생각하지 않습니다. 그의'gameAccessCallback' 함수는 그 중복에있는 문제를 해결합니다. 그는 단지 사건을 제대로 묶는 것이 아닙니다. –