自时间戳起经过的 Javascript 时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15272761/
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
Javascript time passed since timestamp
提问by Parrotmaster
I am trying to "cache" some information by storing it in a variable.
If 2 minutes have passed I want to get the "live" values (call the url).
If 2 minutes have not passed I want to get the data from the variable.
我试图通过将一些信息存储在一个变量中来“缓存”一些信息。
如果 2 分钟过去了,我想获取“实时”值(调用 url)。如果 2 分钟还没有过去,我想从变量中获取数据。
What I basicly want is:
我基本上想要的是:
if(time passed is less than 2 minutes) {
get from variable
} else {
get from url
set the time (for checking if 2 minutes have passed)
}
I've tried calculating the time with things like
我试过用类似的东西来计算时间
if((currentime + 2) < futuretime)
but it wouldn't work for me. Anybody know how to properly check if 2 minutes have passed since the last executing of the code?
但这对我不起作用。有谁知道如何正确检查自上次执行代码以来是否已经过去了 2 分钟?
TL;DR: Want to check if 2 minutes have passed with an IF statement.
TL;DR:想检查 IF 语句是否已经过去了 2 分钟。
回答by rdleal
Turning your algorithm into working javascript, you could do something like this:
将您的算法转换为可运行的 javascript,您可以执行以下操作:
var lastTime = 0;
if ( Math.floor((new Date() - lastTime)/60000) < 2 ) {
// get from variable
} else {
// get from url
lastTime = new Date();
}
You could put the if
block in a function, and call it anytime you want to get the info from either the variable or the url:
您可以将if
块放在一个函数中,并在您想从变量或 url 中获取信息时随时调用它:
var lastTime = 0;
function getInfo() {
if ( Math.floor((new Date() - lastTime)/60000) < 2 ) {
// get from variable
} else {
// get from url
lastTime = new Date();
}
}
Hope it helps.
希望能帮助到你。
回答by tkone
If you want to do something on a timer in JavaScript, you should be using setTimeout
or setInterval
.
如果您想在 JavaScript 中对计时器执行某些操作,您应该使用setTimeout
或setInterval
。
Having your code run in a continuous loop will cause your browser's VM to crash.
让您的代码连续循环运行会导致浏览器的 VM 崩溃。
Using setTimeout
is rather easy:
使用setTimeout
相当简单:
setTimeout(function(){
// do everything you want to do
}, 1000*60*2);
This will cause the function to run in at least two minutes from the time the timeout is set(see this blog post from John Resig for more deatils). The second argument is the number of milliseconds, so we multiply by 60 to get minutes, and then 2 to get 2 minutes.
这将导致该函数在设置超时后至少两分钟内运行(有关更多详细信息,请参阅 John Resig 的这篇博文)。第二个参数是毫秒数,所以我们乘以 60 得到分钟,然后乘以 2 得到 2 分钟。
setInterval
, which follows the same syntax will do something EVERY x milliseconds.
setInterval
, 遵循相同的语法将每 x 毫秒做一些事情。
回答by kamituel
Without using 3rd party libs, just use Date.getTime() and store it as some variable:
不使用 3rd 方库,只需使用 Date.getTime() 并将其存储为某个变量:
var lastRun = null;
function oneIn2Min() {
if (lastRun == null || new Date().getTime() - lastRun > 2000) {
console.log('executed');
}
lastRun = new Date().getTime();
}
oneIn2Min(); // prints 'executed'
oneIn2Min(); // does nothing
oneIn2Min(); // does nothing
setTimeout(oneIn2Min, 2500); // prints 'executed'
You can also opt to make some simple object out of it (to keep your code organised). It could look like this:
您还可以选择从中制作一些简单的对象(以保持代码井井有条)。它可能是这样的:
var CachedCall = function (minTime, cbk) {
this.cbk = cbk;
this.minTime = minTime;
};
CachedCall.prototype = {
lastRun: null,
invoke: function () {
if (this.lastRun == null || new Date().getTime() - this.lastRun > this.minTime) {
this.cbk();
}
this.lastRun = new Date().getTime();
}
};
// CachedCall which will invoke function if last invocation
// was at least 2000 msec ago
var c = new CachedCall(2000, function () {
console.log('executed');
});
c.invoke(); // prints 'executed'
c.invoke(); // prints nothing
c.invoke(); // prints nothing
setTimeout(function () {c.invoke();}, 2300); // prints 'executed'
回答by Thomas Durieux
You can do something like that
你可以做这样的事情
var myVal = {
data: null,
time: new Date()
}
function getMyVal () {
if(myVal.time < new Date(new Date().getTime() - minutes*1000*60)) {
myVal.data = valFromRequest;
myVal=time=new Date();
}
return myVal.data;
}
回答by Ramunas
If you're open to include 3rd party libs this might be very handy in other tasks too: http://momentjs.com/docs/#/manipulating/add/
如果您愿意包含 3rd 方库,这在其他任务中也可能非常方便:http: //momentjs.com/docs/#/manipating/add/