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

121
Visualizações
Checking setInterval every 5 minutes on the 0 second
const secondsInterval = () => {
  const date = getNow();
  if (dayjs(date).minute() % 5 !== 0 && dayjs(date).second() !== 0) {
    console.log("return...");
    return;
  }
  console.log("checking...");
  ...
};
// Check every second, if we're at the 5-minute interval check.
setInterval(secondsInterval, 1000);

This seems to get stuck. It's "checking" on every second of each 5 minute mark. What am I doing wrong? Thanks in advance.

Goal: To "check" every minute and 00 seconds: :00:00, :05:00, :10:00, , :15:00, etc Thanks again.

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

0

You should find out what's the time to your next rounded 5 min. like this:

const FIVE_MIN = 1000 * 60 * 5;

function waitAndDoSomething() {
  const msToNextRounded5Min = FIVE_MIN - (Date.now() % FIVE_MIN);
  console.log(`Waiting ${msToNextRounded5Min}ms. to next rounded 5Min.`);

  setTimeout(() => {
    console.log('It is now rounded 5 min');
    waitAndDoSomething();
  }, msToNextRounded5Min);
}

waitAndDoSomething();
about 4 years ago · Juan Pablo Isaza Relatório

0

If all you care about is executing some code every 5 mins, then don't have it execute every second needlessly, only to return out. Just have it run every 5 mins (300000 MS) and have it do what you need to do and remove all the checking for 5 minute mark code out its unnecessary.

const secondsInterval = () => {

  // -------------------- Remove ----------------
  //const date = getNow();
  //if (dayjs(date).minute() % 5 !== 0 && dayjs(date).second() !== 0) {
  //  console.log("return...");
  //  return;
  //}
  // -------------------- Remove ----------------

  console.log("checking...");
  ...
};
// Check every 5 mins
setInterval(secondsInterval, 300000);
about 4 years ago · Juan Pablo Isaza Relatório

0

Your logic for the if is screwy. Here I reversed it so the if takes care of "do the thing" and the else returns.

const secondsInterval = () => {
  const date = dayjs(new Date());
  if (dayjs(date).minute() % 5 == 0 && dayjs(date).second() == 0) {
    console.log("checking...");
  } else {
    console.log("returning...");
    return;
  }
  //...
};
// Check every second, if we're at the 5-minute interval check.
setInterval(secondsInterval, 1000);
<script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>

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