Javascript setInterval 按分钟 按分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2579762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setInterval By the minute On the minute
提问by bushman
To the javascript enthusiasts,
对于 javascript 爱好者,
how would you program a setTimeOut(or setInterval) handle to fire by the minute on the minute. So for example, if it is the 51 second of the current time, then fire it in 9 seconds, if it is the 14th second then fire it in 46 seconds
您将如何编程setTimeOut(或setInterval)句柄以按分钟触发。例如,如果是当前时间的 51 秒,则在 9 秒内触发,如果是第 14 秒,则在 46 秒后触发
thanks
谢谢
回答by Joel
var date = new Date();
setTimeout(function() {
setInterval(myFunction, 60000);
myFunction();
}, (60 - date.getSeconds()) * 1000);
Obviously this isn't 100% precise, but for the majority of cases it should be sufficient.
显然这不是 100% 精确,但对于大多数情况,它应该足够了。
回答by Pointy
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());
回答by Lajos Arpad
First, let's make sure we know how much time do we have until the next minute:
首先,让我们确保我们知道到下一分钟还有多少时间:
var toExactMinute = 60000 - (new Date().getTime() % 60000);
Now, if you want to work with setTimeout, then
现在,如果你想与 一起工作setTimeout,那么
setTimeout(yourfunction, toExactMinute);
If you want to do a setInterval:
如果你想做一个setInterval:
setTimeout(function() {
setInterval(yourFunction, 60000);
yourFunction();
}, toExactMinute);
回答by Bart?omiej Mucha
At first, I used the solution proposed by Joel Potter. His solution is good enough in most cases. However, because the javascript is doing one thing at a time (unless you are using Workers), it may be late with first timeout, so your first timeout is executed eg. 5 seconds too late and you get interval on every 5 seconds after the minute.
起初,我使用了 Joel Potter 提出的解决方案。在大多数情况下,他的解决方案已经足够好了。但是,因为 javascript 一次只做一件事(除非您正在使用 Workers),所以第一次超时可能会迟到,因此您的第一次超时将被执行,例如。为时已晚 5 秒,您会在一分钟后每 5 秒获得一次间隔。
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();
回答by Stefan Kendall
var seconds = (new Date()).getSeconds();
Use 60-secondsas the timeout time. Granted, setTimeoutlets you specify milliseconds, which means you should also be checking milliseconds, but this will give you "close enough" for most applications. If anything, this should make you overshoot the minute some fraction of a second, but it's better to overshoot than undershoot. Undershooting would cause catastrophic failure with this method.
使用60-seconds的超时时间。当然,setTimeout让您指定毫秒,这意味着您还应该检查毫秒,但这将使您“足够接近”大多数应用程序。如果有的话,这应该会使您在一分钟内超过一分钟,但过冲总比不足好。使用这种方法,欠冲会导致灾难性的失败。
回答by goat
var d = new Date();
var milisecondsUntilMinuteChanges = 60000 - d.getMilliseconds() - (1000 * d.getSeconds());
回答by Prabhakar Poudel
Although too late for the response, I have had similar problem to solve recently and thought of sharing my approach. My problem was to run a timeout after a specified seconds based on how far it is away from a minute, and subsequently run interval function each minute. Here is what I did.
虽然反应太迟了,但我最近有类似的问题需要解决,并想分享我的方法。我的问题是根据距离一分钟的距离在指定秒后运行超时,然后每分钟运行一次间隔函数。这是我所做的。
Let's say the date is javascript date object created using date = new Date();
假设日期是使用创建的javascript日期对象 date = new Date();
- Get the seconds left to reach a minute
secondsLeft = 60 - date.getSeconds(); Then set a timeout function to run after
secondsLeft, and inside the function set an interval function to run each minute as:setTimeout(function() { // perform the action **A**; minuteMS = 60 * 1000; // seconds * milliSeconds setIntervalId = setInterval(function() { // perform the action **A**; }, minuteMS); }, secondsLeft);- Using above format/algorithm you should be able to achieve similar problems be the seconds supplied in any format. The idea is to get the difference of seconds and 60, run a timeout after those seconds, and inside timeout function run interval function with interval as a minute/or anything as such based on requirement.
- 获得剩余的秒数以达到一分钟
secondsLeft = 60 - date.getSeconds(); 然后设置一个超时函数在 之后运行
secondsLeft,并在函数内部设置一个间隔函数以每分钟运行一次:setTimeout(function() { // perform the action **A**; minuteMS = 60 * 1000; // seconds * milliSeconds setIntervalId = setInterval(function() { // perform the action **A**; }, minuteMS); }, secondsLeft);- 使用上述格式/算法,您应该能够以任何格式提供的秒数解决类似的问题。这个想法是获得秒和 60 的差异,在这些秒后运行超时,并在超时函数内部运行间隔函数,间隔为一分钟/或任何基于要求的任何内容。
回答by plodder
function waitForNewMinute() {
if (new Date().getSeconds()>1) {
setTimeout(waitForNewMinute, 500);
} else {
setInterval(doMyThing,60000);
}
}
waitForNewMinute();
回答by CFP Support
I used the ' (60 - date.getSeconds()) * 1000);' method on a data refresh function and, while it worked, it 'drifted' over time (14 seconds late before I changed it!)
我使用了 ' (60 - date.getSeconds()) * 1000);' 数据刷新功能上的方法,当它工作时,它随着时间的推移“漂移”(在我更改它之前晚了 14 秒!)
As the data needed to be updated ON the minute (and that was obviously not happening as we have a clock on the same page!), here's the solution I came up with:
由于数据需要在一分钟内更新(这显然不会发生,因为我们在同一页面上有一个时钟!),这是我想出的解决方案:
setInterval(function () {
if(moment().format('ss') === "00"){
console.log('we are at the 0 - update the data!');
refresh_data();
}
}, 1000);
This of course uses moment.js to find the '0' time and it certainly does what the OP asked - fires on the minute - every time!
这当然使用 moment.js 来查找 '0' 时间,并且它肯定会按照 OP 的要求进行操作 - 每分钟触发一次 - 每次!
回答by Jeremy
I believe all the answers are flawed. Once you have started the setInterval, the interval time cannot be changed without creating a new setInterval object. So you will only use the initial calculated time for each iteration. You can create new setInterval objects, but that has to be costly.
我相信所有的答案都是有缺陷的。一旦启动了 setInterval,如果不创建新的 setInterval 对象,就无法更改间隔时间。因此,您将只使用每次迭代的初始计算时间。您可以创建新的 setInterval 对象,但这代价很高。
See: How do I reset the setInterval timer?
My recommendation is to create a looping function that utilizes the setTimeout instead.
我的建议是创建一个使用 setTimeout 的循环函数。
Here's some code:
这是一些代码:
var duration = 5; //in minutes. This is just to control the total length
var startTime = new Date().getTime(); //must be in milliseconds
function myOnTheMinuteTimer(){
setTimeout(function(){
var currentTimeMS = new Date().getTime();
var diffMs = Math.abs(startTime - currentTimeMS);
diffMs = diffMs/1000; //take out the milliseconds
var seconds = Math.floor(diffMs % 60);
diffMs = diffMs/60;
var minutes = Math.floor(diffMs % 60);
var minutesLeft = duration - minutes;
console.log("Time passed:", minutes + "m :" + seconds + "s");
if(minutes <= duration){
console.log("Timer expired.");
} else {
myOnTheMinuteTimer();
}
}, setIntervalTimeToNextMinute());
}
//Initialize timer
myOnTheMinuteTimer();
//Get our time until the next minute
function setIntervalTimeToNextMinute(){
var currentDateSeconds = new Date().getSeconds();
if(currentDateSeconds == 0){
return 60000;
} else {
return (60 - currentDateSeconds) * 1000;
}
}
My use case for this was a session timout:
我的用例是会话超时:
startTime- allows me to pass in any milliseconds time. This way I can avoid lapses in page loading. Say user logins in at a certain time. I capture that time, then pass it into initial function and it will adjust back to the on minute calculation.
startTime- 允许我传入任何毫秒时间。这样我就可以避免页面加载的失误。假设用户在特定时间登录。我捕获那个时间,然后将它传递给初始函数,它会调整回分钟计算。
duration- I use the duration check for a timeout setting. If the application has a 20 minute timeout, then I need to route the user to the login screen.
持续时间- 我使用持续时间检查进行超时设置。如果应用程序有 20 分钟的超时,那么我需要将用户路由到登录屏幕。
optionally - you can add more variations. Say you wish to give the user a 2 minute warning that they will be logged out. Simply add to the conditional an else if for 2 minutes.
可选 - 您可以添加更多变体。假设您希望给用户 2 分钟的警告,提示他们将被注销。只需将 an else if 添加到条件中 2 分钟。
Hope that helps! Happy coding!
希望有帮助!快乐编码!

