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

271
Visualizações
Creating a loop using setTimeout up to the minute, then every five minutes

I would like to check the minute, until I get to :00, :05, :10, etc minutes, then set to 5 minutes (on the minute) and continue on during the application. Is there a better way to do this? Do I need a clearTimeout? Please help and thanks!

Goal: Check the minutes, once on 00, 05, 10, 15, 30, etc change to 5 minute interval and keep running.

// Set to 1 minute initially.
let interval = 60000;

function timeCheck() {
  const date = getNow();
  console.log("checking...", `${dayjs(date).hour()}:${dayjs(date).minute()}:${dayjs(date).second()}`);
  // Check browser time in minutes for 00, 05, 10, 15, 20, etc.
  if (dayjs(date).minute() % 5 === 0) {
    // Set to 5 minute.
    interval = 300000;
    ...
  }
  setTimeout(timeCheck, interval);
}

clearTimeout(timeCheck);
timeCheck();
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

console.log("Starting...");

const task = () => {
  console.log("do something");
}

let minuteInterval;

const checkMinuteInterval = () => {
  const date = new Date();
  if (date.getMinutes() % 5 === 0) {
    clearInterval(minuteInterval);
    task();
    setInterval(task, 1000 * 60 * 5);
  } else {
    console.log("Not on multiple of 5");
  }
};

minuteInterval = setInterval(checkMinuteInterval, 1000 * 60);
checkMinuteInterval();

about 4 years ago · Juan Pablo Isaza Relatório

0

A completely different approach, since this seems like an XY problem, is to just have an interval for every minute, check if it's a multiple of 5, if so do something, otherwise just break out of the function. (As suggested by @Heretic Monkey)

console.log("Starting...");

const task = () => {
  if ((new Date()).getMinutes % 5 !== 0) {
    console.log("Not a multiple of 5 minutes");
    return; // would just be return; in production code
  }
  
  console.log("Do something")
  // rest of function...
}

task();
setInterval(task, 1000 * 60);

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