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

163
Visualizações
Trigger client side javascript based on datetime

I have an event_start time and I want to run a script 10 seconds after that event start but can't figure out how to trigger the script.

const date = Date().toString();
const currentDateParse = Date.parse(date);
const trigger = Date.parse(startTime) + 1000 * 10

Then this is how I'm attempting to trigger the script but it's not working. How do I start my function when the currentDateParse is equal to trigger. Or, put more simply, 10 seconds after the event starts.

if (currentDateParse = trigger)
   <function code underneath works already>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Calculate the time difference between now and when you want to trigger the script, and use setTimeout() to call it then.

const timediff = trigger - new Date().getTime();
setTimeout(function() {
    // do what you want
}, timediff);
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this (explanations below):

let startTime = "18:00";
let currentTime = new Date();
let target = new Date().parse(startTime);

let timeout = target - currentTime + 1000 * 10; // how long should it wait till the functions is executed

function runTenSecAfterEvent() {
  // do something here
}

setTimeout(runTenSecAfterEvent, timeout);

Explanations

After you calculated target (the start time) and currentTime, you need to know the time difference between them (timeout), so target minus currentTime. After that, you add the 10000ms.

And then you define the function which will be executed 10 seconds after the event occurred (runTenSecAfterEvent()).

Finally, you create a timeout.

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