Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
RemoveEventListener de Javascript no funciona

Ya probé algunas soluciones, como que las funciones de devolución de llamada son las mismas. Todavía no puedo hacer que funcione.

La función removeClickEventHandler dentro del módulo de vista no funciona. El detector de eventos se aplica desde el controlador. Abstraí algo de código del módulo de vista pero dejé el módulo del controlador como está.

Código del controlador:

 import navigationView from "./view/navView.js"; import ticTacToeView from "./view/ticTacToeView.js"; import ticTacModule from "./modules/ticTacModule.js"; class Controller { constructor() { navigationView.addHoverEventHandlers(navigationView.hoverFunction); navigationView.addClickHandler(this.showContent.bind(this)); } showContent() { ticTacToeView.renderContent(navigationView.clickedContent); ticTacToeView.addClickEventHandler(this.ticTacToeControl.bind(this)); ticTacToeView.addHoverHandler(ticTacToeView.hoverFunction); } ticTacToeControl(clickedBox) { if (ticTacToeView.checkIfBoxEmpty(clickedBox)) { ticTacToeView.createMark(clickedBox, ticTacModule.activePlayer); ticTacModule.updateBoardState(clickedBox); ticTacModule.changeActivePlayer(); ticTacToeView.highlightActivePlayer(ticTacModule.activePlayer); ticTacModule.checkForWinner(); if (ticTacModule.winner) { ticTacToeView.renderWinner(ticTacModule.winner); ticTacToeView.removeClickEventHandler(this.ticTacToeControl); } } } }

Este es el código del módulo de vista:

 import View from "./View.js"; class TicTacToeView extends View { addClickEventHandler(fn) { const ticTacContainer = document.querySelector(".tic-tac-toe"); ticTacContainer.addEventListener("click", fn, true); } removeClickEventHandler(fn) { const ticTacContainer = document.querySelector(".tic-tac-toe"); ticTacContainer.removeEventListener("click", fn, true); } } export default new TicTacToeView();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

bind crea una nueva función con una nueva referencia. this.ticTacToeControl y this.ticTacToeControl.bind(this) son dos funciones diferentes con dos referencias diferentes. Almacene la referencia de la función en una variable y utilícela para eliminar el detector de eventos.

 class Controller { constructor() { navigationView.addHoverEventHandlers(navigationView.hoverFunction); navigationView.addClickHandler(this.showContent.bind(this)); } showContent() { ticTacToeView.renderContent(navigationView.clickedContent); this.handler = this.ticTacToeControl.bind(this); ticTacToeView.addClickEventHandler(this.handler); ticTacToeView.addHoverHandler(ticTacToeView.hoverFunction); } ticTacToeControl(clickedBox) { if (ticTacToeView.checkIfBoxEmpty(clickedBox)) { ticTacToeView.createMark(clickedBox, ticTacModule.activePlayer); ticTacModule.updateBoardState(clickedBox); ticTacModule.changeActivePlayer(); ticTacToeView.highlightActivePlayer(ticTacModule.activePlayer); ticTacModule.checkForWinner(); if (ticTacModule.winner) { ticTacToeView.renderWinner(ticTacModule.winner); ticTacToeView.removeClickEventHandler(this.handler); } } } }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda