Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!