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

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

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 Report

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 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!