Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript HTML5 removeEventListener of button fails

New Here ,
Jul 11, 2018 Jul 11, 2018

I have three cases that either addEventListener to five buttons or addEventListener to certain buttons or removeEventListener from all buttons. However, I failed to removeEventListener from the buttons. What's wrong with my code? Please help, thanks!

var set = true;

var showFeedback1 = showFeedback.bind(this, 0);

var showFeedback2 = showFeedback.bind(this, 1);

var showFeedback3 = showFeedback.bind(this, 2);

var showFeedback4 = showFeedback.bind(this, 3);

var showFeedback5 = showFeedback.bind(this, 4);

stage.addEventListener("tick", checkShowFeedback.bind(this))

function checkShowFeedback() {

// s2 currentRound 1 4 7

if (set) {

if (currentRound % 3 == 1) {

console.log("currentRound % 3 == 1");

for (var i = 0; i < 5; i++) {

if (tempS < 60) {

console.log("temps ", i);

this.scoreSheet_label.gotoAndStop(0);

this.scoreSheet_label.visible = true;

switch(i){

case 0:

this.feedbackCategory1.addEventListener("click", showFeedback1);

break;

case 1:

this.feedbackCategory2.addEventListener("click", showFeedback2);

break;

case 2:

this.feedbackCategory3.addEventListener("click", showFeedback3);

break;

case 3:

this.feedbackCategory4.addEventListener("click", showFeedback4);

break;

case 4:

this.feedbackCategory5.addEventListener("click", showFeedback5);

break;

}

}

}

}

// s3 currentRound 2 5 8 9

else if (currentRound % 3 == 2 || currentRound == 9) {

console.log("currentRound % 3 == 2 || currentRound == 9");

this.scoreSheet_label.gotoAndStop(1);

this.scoreSheet_label.visible = true;

this.feedbackCategory1.addEventListener("click", showFeedback1);

this.feedbackCategory2.addEventListener("click", showFeedback2);

this.feedbackCategory3.addEventListener("click", showFeedback3);

this.feedbackCategory4.addEventListener("click", showFeedback4);

this.feedbackCategory5.addEventListener("click", showFeedback5);

}

else {

console.log("else");

this.scoreSheet_label.visible = false;

this.feedbackCategory1.removeEventListener("click", showFeedback1);

this.feedbackCategory2.removeEventListener("click", showFeedback2);

this.feedbackCategory3.removeEventListener("click", showFeedback3);

this.feedbackCategory4.removeEventListener("click", showFeedback4);

this.feedbackCategory5.removeEventListener("click", showFeedback5);

}

set = false;

}

}

function showFeedback(i) {

category = i;

this.left_arrow_scoreSheet.visible = false;

clearGraph();

this.feedback.visible = true;

triggerShowFeedbackSentence = true;

}

441
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 12, 2018 Jul 12, 2018

Well there you go. Every time it enters the frame, the code at the top generates all new showFeedback1, etc. values. This clobbers the previous values, so the removeEventListener calls fail.

Translate
LEGEND ,
Jul 12, 2018 Jul 12, 2018

Does the frame this code is in get returned to multiple times, or does it only execute once?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 12, 2018 Jul 12, 2018

It executes multiple times, i will reload the frame in each round.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 12, 2018 Jul 12, 2018
LATEST

Well there you go. Every time it enters the frame, the code at the top generates all new showFeedback1, etc. values. This clobbers the previous values, so the removeEventListener calls fail.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines