最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript : Replace Event Listener - Stack Overflow

programmeradmin3浏览0评论

I am listening to an event and want to call different methods. For example, I am listening to animation end event and the code is something like this:

this.inAnimationCallback = function() {
    console.log('In');
    _this.elem.className = _this.settings.className;
};
this.outAnimationCallback = function() {
    console.log('Out');
    _this.elem.parentNode.removeChild(_this.elem);
};

this.elem.addEventListener(animationEvent, this.inAnimationCallback);

setTimeout(function() {
    _this.elem.addEventListener(animationEvent, _this.outAnimationCallback);
    // Call some animation here.
}, 3000);

What happens here is that instead of replacing the method attached to the event, JS adds the method and when animation ends, both methods are called. Console looks like this:

(2) In
Out

I am listening to an event and want to call different methods. For example, I am listening to animation end event and the code is something like this:

this.inAnimationCallback = function() {
    console.log('In');
    _this.elem.className = _this.settings.className;
};
this.outAnimationCallback = function() {
    console.log('Out');
    _this.elem.parentNode.removeChild(_this.elem);
};

this.elem.addEventListener(animationEvent, this.inAnimationCallback);

setTimeout(function() {
    _this.elem.addEventListener(animationEvent, _this.outAnimationCallback);
    // Call some animation here.
}, 3000);

What happens here is that instead of replacing the method attached to the event, JS adds the method and when animation ends, both methods are called. Console looks like this:

(2) In
Out
Share Improve this question asked Jun 10, 2015 at 10:52 Talha AkbarTalha Akbar 10k7 gold badges41 silver badges62 bronze badges 3
  • Well you have to call removeEventListener with the other method… – Bergi Commented Jun 10, 2015 at 10:57
  • @Bergi It works! Fk, yeah. I had the idea of removeEventListener but I did something like: this.elem.removeEventListener(animationEvent);. It didn't work but now calling it by passing the method I want to remove results in proper working. Thanks. – Talha Akbar Commented Jun 10, 2015 at 11:01
  • @Bergi Would you like to add answer or should I remove the question? – Talha Akbar Commented Jun 10, 2015 at 11:01
Add a comment  | 

2 Answers 2

Reset to default 13

I'm writing this answer for those like me, who is just started learning JS. And this thread came up first in google to "js replace event listener".. Although, I am not disagreeing with the answers to use removeEventListener(), but mozilla warns that this function is not always successful. So use it with care. not willing to go that road i have found two other ways to do it.

  1. Use something like GlobalEventHandlers which is simple as target.onclick = functionRef;. Mozilla even warns:

    Only one onclick handler can be assigned to an object at a time.

  2. Within listener function add external function call to action function, and then replace reference to another external action function. For example this code will call firstAction(), then seconAction(), then first again...:

        const buttonOne = document.getElementById('buttonOne');
        buttonOne.addEventListener('click', listenerFunction);
    
        let doAction = firstAction; //assigning doAction to firstAction
    
        function listenerFunction() {
            doAction(); //external function call
        }
        function firstAction() {
            doAction = secondAction; //assigning doAction to secondAction
            console.log('first action clicked');
        }
        function secondAction() {
            doAction = firstAction; //assigning doAction to firstAction
            console.log('second action clicked');
        }
        <button type="button" id="buttonOne" name="button">button1</button>

I wrote this answer to broaden solution scope: would have saved at least 6 hours of my time. If I had this in the first place...

You can just remove the event listener before adding the new one :

setTimeout(function() {
    _this.elem.removeEventListener(animationEvent, _this.inAnimationCallback);
    _this.elem.addEventListener(animationEvent, _this.outAnimationCallback);
    // Call some animation here.
}, 3000);
发布评论

评论列表(0)

  1. 暂无评论