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

110
Vistas
Trying to create a function that would create a scheduled post based on time and date

Basically trying to build a message board app that allows users write posts. I want to be able to write posts for the future for example in 5 mins. The problem with this function is that even with setTimeout() and the time difference(seconds) its not working as intended instead its just posting as soon as send the post. I am using firebase and react in this project. messageref refers to the collection!

const sendMessage = async (e) => {
    const now = new Date().getTime()
    console.log(now);
    const seconds = (time - now)/1000
    console.log(seconds);
    e.preventDefault();
    if(formValue === ""){
      alert("must enter a message!")
    }
   
    if(seconds === 0){
      await messageRef
      .add({
        text: formValue,
        timestamp: firebase.firestore.FieldValue.serverTimestamp(),
      })
      setFormValue("")
      pag.current.scrollIntoView({ behavior: "smooth" })
      
    } else if(seconds > 0){
      setTimeout(async () => {
      await messageRef
      .add({
        text: formValue,
        timestamp: time,
      })
      setFormValue("")
      pag.current.scrollIntoView({ behavior: "smooth" })
      }, seconds)
    }else {
      alert('cant send a post in the past')
    }
  };
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The mistake is that you're passing a value in seconds to setTimeout, while that takes a value in milliseconds. So to fix it:

setTimeout(async () => {
  await messageRef
  .add({
    text: formValue,
    timestamp: time,
  })
  setFormValue("")
  pag.current.scrollIntoView({ behavior: "smooth" })
}, seconds*1000)
//          👆

A longer code snippet:

const time = Date.now() + 5000; // in 5 seconds
const formValue = "Hello world";

const now = new Date().getTime()
console.log(now);
const seconds = (time - now)/1000
console.log(seconds);
if(formValue === ""){
  alert("must enter a message!")
}

if(seconds === 0){
  console.log("Sending now");
} else if(seconds > 0){
  console.log("Sending in "+seconds+"s");
  setTimeout(async () => {
    console.log("Sending now");
  }, seconds*1000)
}else {
  alert('cant send a post in the past')
}

I recommend always reproducing problems in this format, as it makes it easiest for folks to help.

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