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

249
Vistas
Disable function when other function is running

I have two event listeners and they are both fired on pageload depending on a video state. But if both are fired, I want the first one to stop running. How is that possible?

obj.addEventListener('suspend', () => {
  // when only this is fired -> run this code
  
  // my code
});

obj.addEventListener('play', () => {
  // when this is fired too -> run this code and deactivate the first code
  
  // my code
});

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

0

You need to define a global variable and check if it is true , not to fire the first function. But there may be some runtime problems , if between running the first event , second event called , first event will not freeze and continues to run.

let shouldRun = true;
obj.addEventListener('suspend', () => {
  // when only this is fired -> run this code

if(!shouldRun){
  return;
}
// my code
});

obj.addEventListener('play', () => {
  // when this is fired too -> run this code and deactivate the first code
  shouldRun = false;
  // my code
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

I guess You can try using obj.removeEventListener('suspend') on the Call of 'play' event.

For more about removeEventListener, you can go through the official link. https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener

about 4 years ago · Juan Pablo Isaza Denunciar

0

A simple way to solve this is to remove the event handler with removeEventListener().

function handleSuspend() {
  // when only this is fired -> run this code
  
  // my code
}

function handlePlay() {
  // when this is fired too -> run this code and deactivate the first code
  obj.removeEventListener('suspend', handleSuspend);
  
  // my code
}

obj.addEventListener('suspend', handleSuspend);
obj.addEventListener('play', handlePlay);
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