在 JavaScript 中检测系统时间的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3367505/
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
Detecting changes to system time in JavaScript
提问by AHOYAHOY
How can I write a script to detect when a user changes their system time in JS?
如何编写脚本来检测用户何时在 JS 中更改系统时间?
采纳答案by strager
There is no (portable) way to track a variable in JavaScript. Also, date information does not lie in the DOM, so you don't get the possibility of a DOM event being triggered.
没有(可移植的)方法来跟踪 JavaScript 中的变量。此外,日期信息不存在于 DOM 中,因此您不会获得触发 DOM 事件的可能性。
The best you can do is to use setIntervalto check periodically (every second?). Example:
您能做的最好的事情是使用setInterval定期检查(每秒?)。 示例:
function timeChanged(delta) {
// Whatever
}
setInterval(function timeChecker() {
var oldTime = timeChecker.oldTime || new Date(),
newTime = new Date(),
timeDiff = newTime - oldTime;
timeChecker.oldTime = newTime;
if (Math.abs(timeDiff) >= 5000) { // Five second leniency
timeChanged(timeDiff);
}
}, 500);
回答by Karel Petranek
Check in an interval function that the time has not changed too much:
检入一个时间没有太大变化的区间函数:
function getTime() {
var d = new Date();
return d.getTime();
}
function checkTime() {
if (Math.abs(getTime() - oldtime) > 2000) { // Changed by more than 2 seconds?
alert("You changed the time!");
}
oldtime = getTime();
}
var oldtime = getTime();
setInterval(checkTime, 1000); // Check every second that the time is not off
Tested on Windows with Opera & FF and works flawlessly.
在带有 Opera 和 FF 的 Windows 上进行了测试,并且可以完美运行。
回答by mattbasta
var last_time = new Date().getTime();
setInterval(function() {
var time = new Date().getTime();
var offset = time - last_time;
if(offset < 0 || offset > 1500) {
// Time has been changed
}
last_time = time;
}, 1000);
In theory, this should work. It will check every second to make sure the time hasn't been changed. Note that I use 1100 milliseconds as most JS interpreters don't fire off events at exactly the time specified.
从理论上讲,这应该有效。它将每秒检查一次以确保时间没有改变。请注意,我使用 1100 毫秒,因为大多数 JS 解释器不会在指定的时间触发事件。
Hope this helps!
希望这可以帮助!
回答by Liam Spencer
Don't think there is a solution to what you are asking for but you can get the users timezone offset.
不要认为您的要求有解决方案,但您可以获得用户时区偏移量。
new Date().getTimezoneOffset() * -1
This returns the offset in minutes from GMT. Bare in mind though this does not take DST into consideration.
这将返回 GMT 的以分钟为单位的偏移量。请记住,尽管这并未考虑 DST。
回答by Nick McCurdy
Do you mean if they are changing their own system time to something that is wrong? You can ask the user for their time zone and get the correct time from the server, and then compare it to the user's system time.
您的意思是他们是否将自己的系统时间更改为错误的内容?您可以询问用户他们的时区并从服务器获取正确的时间,然后将其与用户的系统时间进行比较。
回答by Derrick
You could check every 30 seconds, etc. If the new Time is off by more than 30 seconds +/- some threshold, you could do a more exhaustive comparison to determine how much it has been changed.
您可以每 30 秒检查一次,依此类推。如果新的 Time 关闭超过 30 秒 +/- 某个阈值,您可以进行更详尽的比较以确定它更改了多少。
回答by tianjianchn
use performance.now()to get duration, which will be independent of system clock
用于performance.now()获取持续时间,这将独立于系统时钟
see https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
见https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
And then you can compare performance.now()elapsed with Date.now()elapsed to see whether they are diff too much.
然后你可以将performance.now()elapsed 与Date.now()elapsed 进行比较,看看它们是否差异太大。

