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

291
Visualizações
How to open and close a modal after 3 seconds without Bootstrap or Jquery

I'm new to learning JavaScript and I'm trying to open then close a custom message after 3 seconds of appearing but my function doesn't work. The message appears but does not disappear... can you help me find the solution because I can't find it alone. Thanks very much !

const confirmationMessage = document.getElementById("message_validation");

// launch confirmationMessage form 
const launchConfirmationMessage = () => {
  confirmationMessage.style.display = "flex";
}

//close confirmationMessage form
const closeConfirmationMessage = () => {
  confirmationMessage.style.display = "none";
}

// TimeOut du confirmationMessage
const timeOutConfirmMess = () => {
  setTimeout(closeConfirmationMessage, 3000);
}


// open and close window after 3s
function confirm() {
  if (launchConfirmationMessage() = true && closeConfirmationMessage() === false) {
    timeOutConfirmMess();
  }
}
console.log(confirm());
.message-validation {
  display: none;
  align-items: center;
  justify-content: center;
  position: fixed;
  z-index: 1;
  left: 13%;
  top: 41%;
  overflow: auto;
  background-color: rgba(26, 39, 156, 0.9);
  color: white;
  padding: 5%;
  border-radius: 5px;
}
<div id="message_validation" class="message-validation">
  Bravo! Votre réservation est prise en compte.
</div>

Also, basically the css element is in display:none and it is during a condition that it becomes display:block like this:

if (isOk){ confirm();}

Thanks you again !

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

0

call setTimeout with 3000 milliseconds immediately you show the confirmationMessage. That way, the close function will be called after 3 seconds. confirm function is hence unnecessary, so I removed it. The following works.

const confirmationMessage = document.getElementById("message_validation");

// launch confirmationMessage form 
const launchConfirmationMessage = () => {
  confirmationMessage.style.display = "flex";
  setTimeout(closeConfirmationMessage, 3000);
}

//close confirmationMessage form
const closeConfirmationMessage = () => {
 confirmationMessage.style.display = "none";
}

launchConfirmationMessage();
.message-validation{
  display: none;
  align-items: center;
  justify-content: center;
  position: fixed;
  z-index: 1;
  left: 13%;
  top: 41%;
  overflow: auto;
  background-color: rgba(26, 39, 156, 0.9);
  color: white;
  padding: 5%;
  border-radius: 5px;
}
 <div id="message_validation" class="message-validation">
   Bravo! Votre réservation est prise en compte.
 </div>

Also, in your confirm function, that if statement will never return true because the functions don't return boolean values.

So instead of using the confirm function. Simply call launchConfirmationMessage when isOk is true. I called it at once in the snippet for you to see that it works, but in your codebase you can use the following instead.

if (isOk) {
  launchConfirmationMessage();
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The problem in your code is this if clause:

if (launchConfirmationMessage() = true && closeConfirmationMessage() === false){ 
  // ... 
}

There are more than one errors:

  1. The functions you call do not have any return values, so closeConfirmationMessage() === false will never be true, and launchConfirmationMessage() = true also
  2. You try to assign true to a the call of launchConfirmationMessage: There is only one equal sign instead of tow or three

The following is your code slightly corrected:

const confirmationMessage = document.getElementById("message_validation");

// launch confirmationMessage form 
const launchConfirmationMessage = () => {
  confirmationMessage.style.display = "flex";
}

//close confirmationMessage form
const closeConfirmationMessage = () => {
  confirmationMessage.style.display = "none";
}

// TimeOut du confirmationMessage
const timeOutConfirmMess = () => {
  setTimeout(closeConfirmationMessage, 3000);
}


// open and close window after 3s
function confirm() {
  launchConfirmationMessage();
  
  timeOutConfirmMess();
}
console.log(confirm());
.message-validation {
  display: none;
  align-items: center;
  justify-content: center;
  position: fixed;
  z-index: 1;
  left: 13%;
  top: 41%;
  overflow: auto;
  background-color: rgba(26, 39, 156, 0.9);
  color: white;
  padding: 5%;
  border-radius: 5px;
}
<div id="message_validation" class="message-validation">
  Bravo! Votre réservation est prise en compte.
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

First your if statement is not correct. In a if statement you always compaire two things and you use "==" or "===". You also doesn't return any values in your helper function.

For this code I wouldn't recommend a helper function, the code below will work.

const confirmationMessage = document.getElementById("message_validation");
// open and close window after 3s
function confirm () {
  confirmationMessage.style.display = "flex";
  setTimeout(() => {
      confirmationMessage.style.display = "none";
  }, 3000)

}
  console.log(confirm());

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