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

111
Visualizações
Invoke a function after the time changes

I'm curious if you can invoke a function after the time changes?

For example invoking a function after the minute changes maybe something like this:

onMinChange(()=>{console.log('the minute has changed!')})

Note:

I do not mean waiting for a minute to invoke a function like this

setInterval(()=>{console.log('the minute has changed!')},60000);

there's a difference between

✓ invoking a function after the minute changes (Current time 2:30:05, invoke function at 2:31:00 )

&

X waiting for a minute to invoke a function (Current time 2:30:05, invoke function at 2:31:05 )

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

0

There's nothing built into the web platform that will do this (nor, I think, Node.js's standard library). The closest you can come (without some third-party lib) is to get the time, calculate how long it should be until the next minute, and then use setTimeout to schecule a callback. If you want it repeated, repeat as necessary.

Here's an example:

const MINUTE_IN_MS = 60000;
function callNextMinute(fn) {
    const now = Date.now();
    let next = now / MINUTE_IN_MS;
    const ceil = Math.ceil(next);
    if (ceil > next) {
        next = ceil;
    }
    return setTimeout(fn, (next * MINUTE_IN_MS) - now);
}
function tick() {
    console.log(`New minute! Time is ${new Date()}`);
    callNextMinute(tick);
}
console.log(`Started at ${new Date()}`);
callNextMinute(tick);

There, the margin of error is ~10 seconds.

Beware that:

  1. That kind of recurring execution can have an adverse effect on battery life of mobile devices.
  2. Timers are slowed or even stopped for tabs that aren't the active tab in many browsers, so you may well miss the minute if the tab is inactive.
about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve behaviour like this with the npm package cron.

var job = new CronJob('* * * * *', function() {
  console.log('You will see this message every full minute');
}, null, true, 'America/Los_Angeles');
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