[removed] remove event listener

后端 未结 8 1291
面向向阳花
面向向阳花 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: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.

提交回复
热议问题