Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

298
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda