我正在建立一个简单的脚本,用户点击一个蓝色按钮或一个红色按钮。当点击蓝色按钮时,用户点击的按钮应该淡出,这很好。但是,如果用户点击红色按钮,则淡出的蓝色将停止。就像我说的那样,蓝色按钮可以工作,但红色按钮不能。为什么我的removeEventListener不起作用?
看看这里和其他网站的各种问题和答案我相信我的代码是正确的,似乎它不会工作的原因是因为它们不匹配,即我实际上并没有删除添加事件。
我的代码如下任何帮助将不胜感激,我使用Adobe动画到代码:
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