사용자 클릭이 파란색 버튼 또는 빨간색 버튼 인 간단한 스크립트를 만드는 중입니다. 파란색 버튼을 클릭하면 사용자가 클릭하면 사라지게되어 잘 작동합니다. 그러나 사용자가 빨간색 버튼을 클릭하면 파란색의 페이드 아웃이 중지됩니다. 파란색 버튼은 작동하지만 빨간색은 작동하지 않는다고 말했던 것처럼.removeEventListener가 작동하지 않는 이유는 무엇입니까?
여기 및 다른 사이트에서 다양한 질문과 답변을 볼 때 나는 가지고있는 코드가 정확하다고 믿습니다. 작동하지 않는 이유는 일치하지 않기 때문입니다. 즉 실제로 제거하지 않을 것입니다. 이벤트 추가.
내가 가진 코드는 다음과 도움, 나는 코드 어도비 애니메이션을 사용하고 감상 할 수있다 : 당신이 선택의 여지가
요소의 제거 이벤트 리스너를 들어instance = this;
instance.stop();
//Buttons array
var lowerQuestions = [instance.BTN1, instance.BTN2, instance.BTN4];
//Add an event listener to each button in the array
addEventListeners();
function addEventListeners(){
lowerQuestions.forEach(function(element) {
element.addEventListener("click", function(){
console.log('add listener');
addButtonValue(element);
},false);
});
}
//Remove event listeners when BTN3 is clicked
instance.BTN3.addEventListener("click", removeEventListeners)
function removeEventListeners(){
console.log('prevent');
lowerQuestions.forEach(function(element) {
element.removeEventListener("click", function(){
console.log('remove listener');
addButtonValue(element);
//console.log('hit me here');
},false);
});
}
//Event listener function
function addButtonValue(element){
instance.addEventListener("tick", fadeOut);
element.alpha = 1;
function fadeOut(){
element.alpha -= 0.15;
if(element.alpha <= 0){
instance.removeEventListener("tick", fadeOut);}
}
}
가능한 복제 [있습니까 익명 함수
이 경우 간단한 솔루션은 표준라는 이름의 함수 선언을 사용하여 수신기를 만드는 것입니다 removeEventListener를 처리 할 수 있습니까?] (http://stackoverflow.com/questions/36637197/are-anonymous-functions-able-to-handle-removeeventlistener) – yezzz