Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda