Skip to main content
Inspiring
January 2, 2023
Answered

.on("click", function(){ game.update(); }) does this create a lasting listener?

  • January 2, 2023
  • 1 reply
  • 210 views

Hi team,

So I know if you use     blah_btn.addEventListener you are supposed to remove it when you are done.

If you use this blah_btn..on("click", function(){ game.update(); })

does this leave a listener hanging around? 

 

If it does do you need to remove it/how do you remove it?

Thanks everyone have a great day

    This topic has been closed for replies.
    Correct answer kglad

    "

    If this istn right or there is a better way please let me know - I would be interested if there was a way to take off all listeners from an object without naming them all.

    thanks team

    "

     

    and yes, i don't care for the shortcut listeners, though i recognize other coders, at least, as capable as me do like to use them.

     

    i prefer the move verbose:

     

    addEventListener() and removeEventListener()

     

    but saying verbose is better is going to get a (valid) argument.  it's more a matter of personal preference.  learn one and learn it well and you should have no problems.

    1 reply

    Inspiring
    January 2, 2023

    so I think its just .off('click', function etc

     

    If this istn right or there is a better way please let me know - I would be interested if there was a way to take off all listeners from an object without naming them all.

    thanks team

    Inspiring
    January 2, 2023

    ok so the .off('click'....   does work, but I am still doing something wrong here

    for some reason in the update() function when we get to (this.setstate ==5) or (this.setstate ==6) there is still the listener for game.setState3 on btn_1

    class Game{
        constructor(exportRoot, stage){
            this.root = exportRoot;
            this.stage = stage;
            this.str1 = "start";
            this.strOp1 = "1";
            this.strOp2 = "2";
            this.init();
            this.setstate;
            this.randomNumber;
        }
        
        init(){
            const game = this;
        this.main_Txt = this.root.main_Txt;
        this.op_1 = this.root.op_1;
        this.op_2 = this.root.op_2;
            this.root.start_bt.on("click", function(){ game.update(); })
               this.setstate = 0;
            };
    update(){
        if(this.setstate == 0){
        this.str1 = "You have had a really big weekend camping.  In fact as you are driving home through the dark forest you can hardly keep you eyes open.  The stereo is playing your favourite song but suddenly it cuts out.  There is a huge bright purple light in the sky and your car sputters to a stop.  The stereo is dead and all the lights are out.";
            this.strOp1 = "Try to fix your car";
            this.strOp2 = "Start walking";
            this.root.start_bt.visible = 0;
            this.root.mc_2.visible = false;
             this.root.btn_1.on("click", function(){ game.setstate1(); })
             //this.root.btn_2.on("click", function(){ game.setstate2(); })
        }else if(this.setstate == 1){
           this.str1 = "You get out and open the hood, its hard to see but there is still some purple glow in the sky.  You wonder what that big light came from and what is going on.  All of a sudden there is some moaning and some noises from the underbrush and a zombie stumbles out onto the road.";
            this.strOp1 = "Fight the zombie";
            this.strOp2 = "Ask him to help fix the car";
            this.root.mc_1.visible = false;
            this.root.mc_2.visible = true;
             this.root.btn_1.off("click", function(){ game.setstate1(); })
             this.root.btn_1.on("click", function(){ game.setstate3(); })
             //this.root.btn_2.on("click", function(){ game.setstate2(); }) 
        }else if(this.setstate == 5){
            this.str1 = "You try to fight but the zombie overpowers you and eats your brain.";
            this.strOp1 = "dead";
            this.strOp2 = "dead";
            this.root.mc_1.visible = false;
            this.root.mc_2.visible = false;
            this.root.btn_1.off("click", function(){ game.setstate3(); })
            //this.root.btn_2.on("click", function(){game.setstate2();  })
                 }else if(this.setstate == 6){
            this.str1 = "lost.";
            this.strOp1 = "dead";
            this.strOp2 = "dead";
            this.root.mc_1.visible = false;
            this.root.mc_2.visible = false;
            this.root.btn_1.off("click", function(){ game.setstate3(); })
             //this.root.btn_2.on("click", function(){ game.setstate2(); }
                 }
        this.setText();
    }
        chooseNumber() {
    	this.randomNumber = Math.floor(Math.random() * 10) + 1;
        }
        setText(){
        this.main_Txt.text = this.str1;
        this.op_1.text = this.strOp1;
        this.op_2.text = this.strOp2;
        }
        setstate1(){
        
        console.log('setstate1');
        this.setstate = 1;
        this.update();    
        }
        setstate3(){
            
            this.chooseNumber();
            if(this.randomNumber >= 5){
        console.log('setstate5');
        this.setstate = 5;
        this.update();   
             
            }else{
        console.log('setstate6');
        this.setstate = 6;
        this.update();  
            }
            
        
        }
      
        }

    If I put the .off('click' earlier and kill both listeners back in the (this.setState == 1) the listeners seem to be gone and the button is inactive.  

    How the code is at the moment, the button continues to call setState3() function

    kglad
    Community Expert
    Community Expert
    January 2, 2023

    the op found a work-around, but for others (and the op):

     

    // add shortcut listener

    var btn_2_closure = this.root.btn_2.on("click", function(){ game.setstate2(); })

     

    // remove shortcut listener

    this.root.btn_2.off("click", btn_2_closure)