Delete events from the document element

3

I have several listeners. Each time my file is executed with this code, it is listened to and executed n times, according to the number of times it uses the .js file. I would like to eliminate all events so that it only works once.

 document.addEventListener("clmtrackrNotFound", function(event) {
        //bla bla
    }, false);

   // detect if tracker loses tracking of face
    document.addEventListener("clmtrackrLost", function(event) {
        //bla bla
    }, false);

    // detect if tracker has converged
    document.addEventListener("clmtrackrConverged", function(event ) {
   },false);

How can I delete all the listener events the sun element?

This I'm doing according to @sakulino's response

     function fn_clmtrackrConverged(event) {
      event.target.removeEventListener(event.type, "fn_clmtrackrConverged");
     //bla bla bla
     //final del codigo
     event.target.removeEventListener(event.type, "fn_clmtrackrConverged");

 }

 document.addEventListener("clmtrackrConverged","fn_clmtrackrConverged"); 
 fn_clmtrackrConverged(event);
    
asked by yavg 25.07.2017 в 18:15
source

1 answer

3

You could use event.target.removeEventListener(event.type, nombredelafuncion); , you have to remove the EventListener and then execute the code that you want to run only once.

Your code would remain that way (I recommend that you do not use anonymous functions, but do a function and pass the name to the eventListener):

    function funcion1(event) {
        event.target.removeEventListener(event.type, "funcion1");
        //bla bla
    }

    document.addEventListener("clmtrackrNotFound","funcion1"); 

The same for each event.

Documentation.

    
answered by 25.07.2017 / 18:26
source