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

156
Visualizações
JS a function that is executed only once
var template = document.querySelector('#template');
var popup = document.querySelector('.popup-bg');
 
template.addEventListener('mouseleave', function(){
  popup.style.display = "block";
}, { once: true });

Hello. A function that displays a popup when the mouse cursor exits the element. It is supposed to work on every subpage, but it is supposed to work only once. The problem is that when you enter another subpage, it will reload and work again.

It is enough for the function to work only once, regardless of the change of the subpage in one session. It may work again (once) when it is visited or refreshed again.

I need an idea how to add a function that will work once and will not reload, or put an element / template on the entire window, which will not reload after changing subpages and refer to it in the function.

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

0

You can save the flag in session storage to achieve the result you want like in this example


var template = document.querySelector('#template');
var popup = document.querySelector('.popup-bg');

if (!sessionStorage.getItem('popup')) {
  sessionStorage.setItem('popup', 'set');

  template.addEventListener('mouseleave', function(){
    popup.style.display = "block";
  }, { once: true });
}
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