SetInterval Per Minute Per Minute

For javascript enthusiasts,

how would you program the setTimeOut (or setInterval ) handle to fire from minute to minute. So, for example, if it is 51 seconds of the current time, then run it in 9 seconds, if it is the 14th second, and then run it in 46 seconds.

thanks

+11
javascript settimeout
source share
9 answers
 var date = new Date(); setTimeout(function() { setInterval(myFunction, 60000); myFunction(); }, (60 - date.getSeconds()) * 1000); 

Obviously, this is not 100% accurate, but for most cases this should be enough.

+17
source share
 var nextTick = function() { return 60000 - (new Date().getTime() % 60000); }, timerFunction = function() { // do stuff // do stuff // do stuff setTimeout(timerFunction, nextTick()); }; var timeout = setTimeout(timerFunction, nextTick()); 
+6
source share

First, let's make sure we know how much time we have until the next minute:

 var toExactMinute = 60000 - (new Date().getTime() % 60000); 

Now, if you want to work with setTimeout , then

 setTimeout(yourfunction, toExactMinute); 

If you want to do setInterval :

 setTimeout(function() { setInterval(yourFunction, 60000); yourFunction(); }, toExactMinute); 
+3
source share
 var seconds = (new Date()).getSeconds(); 

Use 60-seconds as a timeout. Of course, setTimeout allows you to specify milliseconds, which means you should also check milliseconds, but this will give you “close enough” for most applications. In any case, this should make you overshoot the minute for a fraction of a second, but it’s better to overshoot than malaise. Deficiencies can lead to a catastrophic failure of this method.

+1
source share

At first, I used the solution suggested by Joel Potter. Its solution is good enough in most cases. However, since javascript does one thing at a time (unless you use Workers), it may be late with the first timeout, so your first timeout is executed, for example. 5 seconds is too late, and you get an interval every 5 seconds after a minute.

So here is my implementation:

 function setMyFunctionInterval() { var currentDateSeconds = new Date().getSeconds(); if (currentDateSeconds == 0) { setInterval(myFunction, 60000); } else { setTimeout(function () { setMyFunctionInterval(); }, (60 - currentDateSeconds) * 1000); } myFunction(); } setMyFunctionInterval(); 
+1
source share
 var d = new Date(); var milisecondsUntilMinuteChanges = 60000 - d.getMilliseconds() - (1000 * d.getSeconds()); 
0
source share
 function waitForNewMinute() { if (new Date().getSeconds()>1) { setTimeout(waitForNewMinute, 500); } else { setInterval(doMyThing,60000); } } waitForNewMinute(); 
0
source share

Although it is too late for an answer, I had a similar problem to solve recently and thought about sharing my approach. My problem was to start a timeout after certain seconds, based on how far it is away from the minute, and then every minute it performs the interval function. Here is what I did.

Let's say a date is a javascript date object created using date = new Date();

  • Get the remaining seconds to reach the minute secondsLeft = 60 - date.getSeconds();
  • Then set the timeout function to run after secondsLeft , and inside the function, set the interval function to run every minute as:

     setTimeout(function() { // perform the action **A**; minuteMS = 60 * 1000; // seconds * milliSeconds setIntervalId = setInterval(function() { // perform the action **A**; }, minuteMS); }, secondsLeft); 
  • Using the above format / algorithm, you should be able to achieve similar problems, be it the seconds provided in any format. The idea is to get the difference in seconds and 60, start a timeout after these seconds and a timeout interval function with an interval like minute / or something as such based on the requirement.
0
source share

I used '(60 - date.getSeconds ()) * 1000);' the data refresh function method, and while it worked, it “drifted” over time (14 seconds later than I changed it!)

Since the data needed to be updated in a minute (and this obviously was not, since we have a clock on one page!), Here is the solution I found:

 setInterval(function () { if(moment().format('ss') === "00"){ console.log('we are at the 0 - update the data!'); refresh_data(); } }, 1000); 

This, of course, uses the .js moment to find the time “0”, and it certainly does what the OP asked for - it works per minute - every time!

0
source share

All Articles