[removed] remove event listener

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

    If @Cybernate's solution doesn't work, try breaking the trigger off in to it's own function so you can reference it.

    clickHandler = function(event){
      if (click++ == 49)
        canvas.removeEventListener('click',clickHandler);
    }
    canvas.addEventListener('click',clickHandler);
    
    0 讨论(0)
  • 2020-11-22 05:48

    If someone uses jquery, he can do it like this :

    var click_count = 0;
    $( "canvas" ).bind( "click", function( event ) {
        //do whatever you want
        click_count++;
        if ( click_count == 50 ) {
            //remove the event
            $( this ).unbind( event );
        }
    });
    

    Hope that it can help someone. Note that the answer given by @user113716 work nicely :)

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