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

109
Visualizações
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 Respostas
Responde à pergunta

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 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