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

164
Visualizações
How to prevent function/event from firing multiple times

There is an issue with the createResetHandler function.

The createResetHandler function event fires multiple times.

Seen Here: https://jsfiddle.net/oxjzmk18/

To reproduce, when the exit button appears on the screen, click it multiple times.

Clicking on the exit button multiple times causes the event to be fired multiple times.

You have to mouse click the exit button more than 1 time.

How do I prevent that, so that it only fires 1 time?

How is that fixed in the code?

  function createResetHandler(player) {
    const resetVideo = document.querySelectorAll(".exit");
    resetVideo.forEach(function resetVideoHandler(video) {
      video.addEventListener("click", function resetVideoHandler() {
        player.destroy();
        console.log("createResetHandler");
      });
    })
  }

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This could work.

Attach event handler to the active exit button only, document.querySelector could be better choice in this case.

const resetVideo = document.querySelectorAll(".container.active .exit");

attach event with once: true, so event handler is removed once the event is fired.

  function createResetHandler(player) {

    const resetVideo = document.querySelectorAll(".container.active .exit");
    resetVideo.forEach(function resetVideoHandler(video) {
      video.addEventListener("click", function resetVideoHandler() {
        player.destroy();
        console.log("removePlayer");
      }, {
        once: true
      });
    })
  }

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

  • once

A boolean value indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.

Or using removeEventListener

  function createResetHandler(player) {

    const resetVideo = document.querySelectorAll(".container.active .exit");
    resetVideo.forEach(function resetVideoHandler(video) {
      const resetVideoHandler = () => {
         video.removeEventListener("click", resetVideoHandler);
          player.destroy();
          console.log("removePlayer");
      };
      video.addEventListener("click", resetVideoHandler);
    })
  }
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