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

137
Vistas
Disable an addEventListener function when needed

This function is needed for some time. But when tasks are completed I need to disable it, that after clicking on 'board' it wouldn't work.

board.addEventListener('click', event => {
    if (event.target.classList.contains('circle')) {
        score++
        event.target.remove()
        createRandomCircle()
    } else if (!event.target.classList.contains('circle')) {
        fault++
    }
})

//Here I want to deactivate this event listener

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I would call a handler function that returns a new named function (a closure) to act as the listener. You can then go through your tasks, and remove the listener when they're complete.

const board = document.querySelector('#board');

board.addEventListener('click', handler(), false);

function handler() {

  // This is the function the listener uses
  return function listener(e) {
    console.log(e.target);
    console.log('Task one');
    console.log('Task two');
    board.removeEventListener('click', listener);
    console.log('The button no longer works.');
  }

}
<button id="board">Board</button>

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