[removed] remove event listener

后端 未结 8 1288
面向向阳花
面向向阳花 2020-11-22 04:58

I\'m trying to remove an event listener inside of a listener definition:

canvas.addEventListener(\'click\', function(event) {
    click++;
    if(click == 50         


        
相关标签:
8条回答
  • 2020-11-22 05:36

    You need to use named functions.

    Also, the click variable needs to be outside the handler to increment.

    var click_count = 0;
    
    function myClick(event) {
        click_count++;
        if(click_count == 50) {
           // to remove
           canvas.removeEventListener('click', myClick);
        }
    }
    
    // to add
    canvas.addEventListener('click', myClick);
    

    EDIT: You could close around the click_counter variable like this:

    var myClick = (function( click_count ) {
        var handler = function(event) {
            click_count++;
            if(click_count == 50) {
               // to remove
               canvas.removeEventListener('click', handler);
            }
        };
        return handler;
    })( 0 );
    
    // to add
    canvas.addEventListener('click', myClick);
    

    This way you can increment the counter across several elements.


    If you don't want that, and want each one to have its own counter, then do this:

    var myClick = function( click_count ) {
        var handler = function(event) {
            click_count++;
            if(click_count == 50) {
               // to remove
               canvas.removeEventListener('click', handler);
            }
        };
        return handler;
    };
    
    // to add
    canvas.addEventListener('click', myClick( 0 ));
    

    EDIT: I had forgotten to name the handler being returned in the last two versions. Fixed.

    0 讨论(0)
  • 2020-11-22 05:36

    You could use a named function expression (in this case the function is named abc), like so:

    let click = 0;
    canvas.addEventListener('click', function abc(event) {
        click++;
        if (click >= 50) {
            // remove event listener function `abc`
            canvas.removeEventListener('click', abc);
        }
        // More code here ...
    }
    

    Quick and dirty working example: http://jsfiddle.net/8qvdmLz5/2/.

    More information about named function expressions: http://kangax.github.io/nfe/.

    0 讨论(0)
  • 2020-11-22 05:37
       canvas.addEventListener('click', function(event) {
          click++;
          if(click == 50) {
              this.removeEventListener('click',arguments.callee,false);
          }
    

    Should do it.

    0 讨论(0)
  • 2020-11-22 05:37
    element.querySelector('.addDoor').onEvent('click', function (e) { });
    element.querySelector('.addDoor').removeListeners();
    
    
    HTMLElement.prototype.onEvent = function (eventType, callBack, useCapture) {
    this.addEventListener(eventType, callBack, useCapture);
    if (!this.myListeners) {
        this.myListeners = [];
    };
    this.myListeners.push({ eType: eventType, callBack: callBack });
    return this;
    };
    
    
    HTMLElement.prototype.removeListeners = function () {
    if (this.myListeners) {
        for (var i = 0; i < this.myListeners.length; i++) {
            this.removeEventListener(this.myListeners[i].eType, this.myListeners[i].callBack);
        };
       delete this.myListeners;
    };
    };
    
    0 讨论(0)
  • 2020-11-22 05:40

    Try this, it worked for me.

    <button id="btn">Click</button>
    <script>
     console.log(btn)
     let f;
     btn.addEventListener('click', f=function(event) {
     console.log('Click')
     console.log(f)
     this.removeEventListener('click',f)
     console.log('Event removed')
    })  
    </script>
    
    0 讨论(0)
  • 2020-11-22 05:44

    I think you may need to define the handler function ahead of time, like so:

    var myHandler = function(event) {
        click++; 
        if(click == 50) { 
            this.removeEventListener('click', myHandler);
        } 
    }
    canvas.addEventListener('click', myHandler);
    

    This will allow you to remove the handler by name from within itself.

    0 讨论(0)
提交回复
热议问题