Take the 2-minute tour ×

How could I accurately run a function when the minute changes? Using a setInterval could work if I trigger it right when the minute changes. But I'm worried setInterval could get disrupted by the event-loop in a long-running process and not stay in sync with the clock.

How can I run a function accurately when the minute changes?

share|improve this question

5 Answers 5

up vote 4 down vote accepted

First off, you should use setInterval for repeating timers, since it (tries to) guarantee periodic execution, i.e. any potential delays will not stack up as they will with repeated setTimeout calls. This will execute your function every minute:

function f() {
  console.log(new Date);

setInterval(f, 60 * 1000);

Now, what we need to do is to start this at the exact right time:

function repeatEvery(func, interval) {
    // Check current time and calculate the delay until next interval
    var now = new Date,
        delay = interval - now % interval;

    function start() {
        // Execute function now...
        // ... and every interval
        setInterval(func, interval);

    // Delay execution until it's an even interval
    setTimeout(start, delay);

repeatEvery(f, 60 * 1000);
share|improve this answer

You can try to be as accurate as you can, setting a timeout each X milliseconds and check if the minute has passed and how much time has passed since the last invocation of the function, but that's about it.

You cannot be 100% sure that your function will trigger exactly after 1 minute, because there might be something blocking the event-loop then.

If it's something vital, I suggest using a cronjob or a separate Node.js process specifically for that (so you can make sure the event loop isn't blocked).



share|improve this answer
You may have to read the comment of MMJ within the linked article. –  KooiInc May 29 '12 at 8:26

This may be an idea. The maximum deviation should be 1 second. If you want it to be more precise, lower the milliseconds of setTimeout1.


function checkMinutes(){
  var now = new Date().getMinutes();
  if (now > checkMinutes.prevTime){
    // do something
    console.log('nextminute arrived');
  checkMinutes.prevTime = now;

1 But, see also this question, about accuracy of timeouts in javascript

share|improve this answer
If in his application a callback from a previous function gets triggered just before checkChange that callback might take 1-2 seconds (if he needs to process something on the spot), so he cannot be 100% sure, right? –  alessioalex May 29 '12 at 8:17
True. That's why I added a footnote. Aside from that: a method taking 1-2 seconds processing time would trigger me to optimize my scripting. –  KooiInc May 29 '12 at 8:23
+1 nice simple example .... –  ManseUK May 29 '12 at 8:48

My first thought would be to use the Date object to get the current time. This would allow you to set your set interval on the minute with some simple math. Then since your worried about it getting off, every 5-10 min or whatever you think is appropriate, you could recheck the time using a new date object and readjust your set interval accordingly.

This is just my first thought though in the morning I can put up some code(its like 2am here).

share|improve this answer

I've put up a possible solution for you:

/* Usage:
 * coolerInterval( func, interval, triggerOnceEvery);
 *   - func : the function to trigger
 *   - interval : interval that will adjust itself overtime checking the clock time
 *   - triggerOnceEvery : trigger your function once after X adjustments (default to 1)
var coolerInterval = function(func, interval, triggerOnceEvery) {

    var startTime = new Date().getTime(),
        nextTick = startTime,
        count = 0;

    triggerOnceEvery = triggerOnceEvery || 1;

    var internalInterval = function() {

        nextTick += interval;

        if(count == triggerOnceEvery) {

            count = 0;

        setTimeout(internalInterval, nextTick - new Date().getTime());



The following is a sample usage that prints the timestamp once every minute, but the time drift is adjusted every second

coolerInterval(function() {

    console.log( new Date().getTime() );

}, 1000, 60);

It's not perfect, but should be reliable enough. Consider that the user could switch the tab on the browser, or your code could have some other blocking tasks running on the page, so a browser solution will never be perfect, it's up to you (and your requirements) to decide if it's reliable enough or not.

share|improve this answer

Your Answer


