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

118
Visualizações
How to prevents events from firing multiple time

For some reason I have to call the same function twice because of that my event are firing twice. How can I prevent this?

function renderSeats(movieImg, title, release_date, dummyPrice = '3000') {
     
      // selected items
       //this is only a part of the code . 
      contianer.addEventListener('click', function (e) {
        if (
          e.target.classList.contains('seat') &&
          !e.target.classList.contains('unavailable')
        ) {
          e.target.classList.toggle('selected');
          payoutBtn.classList.add('active');
          console.log('yes');
          updateCountTotal(); //function call
        }
        contianer.setAttribute('listener', 'true');
      });
    
      const ticketOverlayClose = document.querySelector('.ticket__close-svg');
      // overlay deactive
      ticketOverlayClose.addEventListener('click', function (e) {
        overlayseats.classList.add('hidden');
        payoutBtn.classList.add('active');
        ticketContainer.classList.remove('active');
      });
    }

Every time I am calling the function from another function because I have to update the dom, suppose 3 times the event then also fire 3 times

I also attached an image. In the console, you see after I clicking back (it is a single-page app) and again load by page calling our rendered seat function, and this time when I click on the seats the even is firing twice.

[gif][1]:https://i.stack.imgur.com/Ik1CG.gif

about 4 years ago · Juan Pablo Isaza
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