需要Firefox Date()错误的解决方法 [英] Need workaround for Firefox Date() bug
问题描述
我正在处理一个画布图,该图使用我们向客户显示的信息进行实时更新,并且正在准备DST的更改。我们的要求之一是让图表能够像往常一样正常运行,而无需客户在时钟切换时刷新页面。
I'm working on a canvas graph that's updated in real time with information we're displaying to a customer, and were in the process of preparing for the DST change on the clocks. One of our requirements is for the graph to carry on functioning as usual without the need for the customer to refresh the page when the clocks switch over.
解决此问题时,我发现了有关Firefox的错误:
While working on this problem, I found out about this bug with Firefox:
https://bugzilla.mozilla.org/show_bug.cgi?id=127246
JavaScript中的Date()对象基本上不会如果在不关闭浏览器/选项卡的情况下更改了系统时间,则在Firefox中进行更新,并且由于我们正在使用系统时钟查询API,因此这是一个相当大的问题。
Basically the Date() object in JavaScript doesn't update in Firefox if the system time is changed without having to close the browser/tab, and as we're querying an API using the system clock, this is a pretty major problem.
我假设它没有固定,因为票证仍被标记为 NEW,而且我也很确定是导致问题的原因,而不是我的代码的另一部分,所以我怎么能得到最新的在Firefox中更改而无需刷新页面后系统时钟的时间?
I'm assuming it's not fixed as the ticket is still marked as 'NEW', and I'm also pretty sure it's this that's causing the problem rather than another part of my code, so how can i get the current time of the system clock after it changes in Firefox without having to refresh the page?
仅供参考,我正在使用的Firefox版本是19.0.2
FYI the version of Firefox I'm using is 19.0.2
预先感谢
示例
将系统时钟设置为12:00并打开Web应用...
Set system clock to 12:00 and open web app...
var currentHour = new Date().getHours() //returns 12
将系统时钟设置为13:00重新打开Web应用程序。
Set system clock to 13:00 without reopening web app..
var currentHour = new Date().getHours() //returns 12
推荐答案
使用网络工作者
而不是像 Sergiu Toarca的答案中那样创建新窗口,每次需要更新时都创建一个新的网络工作者。每当生成新的网络工作者时,Firefox都会更新 Date()
对象。
Use Web Workers
Instead of creating a new window as in Sergiu Toarca's answer, create a new web worker every time you need an update. Firefox would update the Date()
object whenever a new web worker is generated.
function currDate() {
var blob = new Blob([""]),
blobURL = window.URL ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob),
worker = new Worker(blobURL);
worker.terminate();
return new Date();
}
您可以像普通的 Date()一样使用它
对象:
currDate().getHours(); // Gives you an updated hour
请参见演示(在Firefox 19上运行)。
See DEMO (Works on Firefox 19).
这篇关于需要Firefox Date()错误的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!