self Kill EventListener ? (SOLVED)

Discussion in 'Learning Javascript' started by Jonforum, Aug 1, 2017.

  1. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,577
    Likes Received:
    1,348
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    hi it is possible to self kill a EventListener ?
    If yes how i can performe this ?

    example:
    PHP:
        vidControl.vIntro.addEventListener("timeupdate", function() { 
            if (
    this.currentTime 10) {
                
    //Do Stuff one time
                
    this.removeEventListener('timeupdate',this); // And Do Self KILL, BREAK or DESTROY ?
            
    }
        }, 
    false);
    thank
     
    #1
  2. LTN Games

    LTN Games Veteran Veteran

    Messages:
    609
    Likes Received:
    459
    Location:
    Canada
    First Language:
    English
    Primarily Uses:
    RMMV
    Try putting the function in a variable and using that to add and remove the listener, I don't think you can remove an event listener if it's an anonymous function.

    I'd type some code for you but I am on mobile phone. :/
     
    #2
    Clock Out likes this.
  3. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,577
    Likes Received:
    1,348
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    humm something like thats ?
    i give name to the anonyme function in the listener, then remove it when the function has run:
    PHP:
        vidControl.vIntro.addEventListener("timeupdate", function TESTT() {
            if (
    this.currentTime 10) {
                
    //Do Stuff one time
                
    this.removeEventListener('timeupdate',TESTT); // work! But is this the right way to do it? 
            
    }
        }, 
    false);
     
    #3
  4. LTN Games

    LTN Games Veteran Veteran

    Messages:
    609
    Likes Received:
    459
    Location:
    Canada
    First Language:
    English
    Primarily Uses:
    RMMV
    That is one way of doing it, absolutely, this works for you correct? My initial thought was to assign it to a variable but the way you did it should work fine. There is no one way of doing things, there is usually plenty of ways to achieve the same result.
    Code:
    // My initial thought was....
    
    var updateTimeCall = function () {
    //Do stuff and remove listener by refrencing the variable name
    removeEventListener('event', updateTimeCall)
    }
    addEventListener('event', updateTimeCall)
     
    #4
    Jonforum likes this.
  5. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,577
    Likes Received:
    1,348
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    yes is work
    i found tips in this article
    https://www.broken-links.com/2013/05/22/removing-event-listeners-with-anonymous-functions/
     
    #5
    LTN Games likes this.

Share This Page