Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda