Skip to main content
Inspiring
August 19, 2017
Question

Functions question in Animate Canvas

  • August 19, 2017
  • 1 reply
  • 260 views

I’m a little confused regarding communication between functions as the code below throws an undefined error.

What I’m trying to accomplish, is a simple way that I can turn a group of EventListeners on and off. - Peter

function startListen() {

  var binderbp1 = bp1_click.bind(this);

  this.bp1.addEventListener(“click", binderbp1);

  var binderbp2 = bp1_click.bind(this);

  this.bp1.addEventListener(“click", binderbp2);

  var binderbp3 = bp1_click.bind(this);

  this.bp1.addEventListener(“click", binderbp3);

}

function endListen() {

  this.bp1.removeEventListener("click", binderbp1);

  this.bp2.removeEventListener("click", binderbp2);

  this.bp3.removeEventListener("click", binderbp3);

}

startListen();

endListen();

This topic has been closed for replies.

1 reply

Legend
August 19, 2017

binderbp1, etc, are undefined in endListen() because you defined them as local variables in startListen(). If you want them visible to both functions, define them outside both functions.

Or you could dispense with the variables entirely and just use .removeAllEventListeners("click").