为什么移动浏览器似乎提供了与台式机浏览器不同的Javascript Date()结果? [英] Why does mobile safari seem to give different Javascript Date() results from desktop browsers?

查看:96
本文介绍了为什么移动浏览器似乎提供了与台式机浏览器不同的Javascript Date()结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我完全无法解释.为了描述问题,我需要描述我的Web应用程序这一特定部分的流程.

I have a strange problem that I am at a complete loss to explain. In order to describe the problem, I need to describe the flow of this particular part of my webapp.

想象一个设备,在这种情况下是运行现代浏览器的台式PC.连接此用户后,它将充当主用户.该主站像这样检索javascript中的当前时间:

Imagine one device, in this case a desktop PC running a modern browser. When this user is connected, it acts as the master. This master retrieves the current time in javascript like so:

get_time : function() {
              var d = new Date();
              return d.getTime() - this.cache.start_time;
              //We have tried using the modern Date.now method also but to no avail
              //return Date.now() - this.cache.start_time;
},

这一次通过网络套接字发送到node.js中,并被临时存储在服务器端以用于此特定流.

This time is sent via a websocket into node.js and is temporarily stored server side for this particular stream.

现在想象一下其他几种设备,包括手机,平板电脑和其他台式计算机的混合物.从本质上讲,这些设备成为主设备的从设备,我们要求它们需要及时同步.为了实现这一点,当这些从属服务器连接到node.js服务器时,将保存start_time从该流的主服务器推送到从属服务器.

Now imagine several other devices, a mixture of mobile phones, tablets and other desktop computers. These devices in essence become slaves to the master and we require they need to sync up timewise. In order to achieve this, when these slaves connect the node.js server pushes the saves start_time from the master of this stream into the slaves.

如上面的函数所示,此变量变为属性this.cache.start_time.

This variable becomes the property this.cache.start_time as can be seen in the function above.

从这个开始时间开始,通过从开始时间中减去从属浏览器中的当前时间来进行同步.

From this start time syncopation takes place by subtracting the current time in the slave's browser from the start time.

需要执行此操作的原因与该问题无关,因此我将省略它们.但是,我将通过解释每个从属服务器上正在进行的处理来添加一些细节,这些处理仅需要从与主服务器当前正在处理的数组索引完全相同的数组索引开始.通过在主机及其所有从机上同步start_time变量,我们可以获得正确的数组索引并从那里开始基于时间的处理.

The reasons for needing to do this are irrelevant to this question, and as such I will omit them. However, I will add a little detail by explaining that there is ongoing processing going on on each of the slaves that simply needs to start at exactly the same array index as that which the master is currently processing. By syncing the start_time variable across the master and all of its slaves, we can obtain the correct array index and begin time based processing from there.

我的问题是,除了移动Safari(我已经测试过iOS 6和iOS 7 beta2)之外,我尝试使用的每种浏览器中,所有从属设备都与主设备完美同步.

The problem I have is that in every browser I try with the exception of mobile Safari (I have tested iOS 6 and iOS 7 beta2), all of the slaves sync up perfectly with the master.

正如您在上面的代码中看到的那样,我尝试使用Date.Now()方法,但无济于事.通过监视node.js,我可以看到服务器正在将正确的start_time推送到我的iphone中,但是每次在移动safari中修改变量时的结果似乎都不同了几秒钟.

As you can see in the code above I have tried using the Date.Now() method instead, but to no avail. From monitoring node.js I can see that the server is pushing the correct start_time into my iphone, yet the result when modifying the variable in mobile safari seems to come out several seconds different every time.

让事情变得更加困难,我目前无法使用Apple计算机,因此无法正确调试移动Safari以了解发生了什么情况.

To make matters more difficult, I do not currently have access to an Apple computer and as such am unable to properly debug mobile Safari to understand what is going on.

我完全不知所措,无法理解为什么Apple的javascript Date实现可能或如何将不同的结果返回给其他浏览器.只是没有道理.

I am at a complete loss and cannot understand why or how Apple's javascript Date implementation could be throwing back different results to other browsers. It just doesn't make sense.

在使用移动Safari时,我是否不了解某些内容? Apple用javascript获取当前时间的方法是否有任何已知的区别?关于如何才能准确地收集跨浏览器的当前时间还有其他建议吗?

Is there something I am unaware of with regards to working with mobile Safari? Are there any known differences with Apple's method of getting the current time in javascript? Are there any other suggestions for how I could accurately gather the current time that is consistent across browsers?

预先感谢您的任何建议或意见.

Thanks in advance for any suggestions or advice.

编辑:我刚刚有一个很清醒的想法.当您在javascript中获得时间时,它是否使用客户端系统时钟?就是说,如果他们离开了几秒钟,那将是无知的吗?即主时间可能是16:31:22,而iPhone时间可能是16:31:16.

I've just had a rather sobering thought. When you get the time in javascript does it use the clients system clock? Meaning that if they are out by even a few seconds this will be innacurate? i.e. Master time might be 16:31:22 whereas iPhone time might be 16:31:16.

如果是这种情况,那么我需要重新考虑如何最好地同步这些设备.如果确实是造成这种差异的原因,我当然会接受它作为答案.

If that is the case, then I need a complete rethink about how best to sync up these devices. If that is indeed the reason for the difference I will of course accept that as the answer.

推荐答案

正如我在上面的编辑中所提到的,该问题原来是由于本地系统时间因设备而异.

As Mentioned in my EDIT above, the problem turned out to be due to local system times differing across devices.

我天真地认为这归因于移动safari,因为我们拥有的所有台式机设备均会自动进行时间同步,因此它们似乎都可以正常工作.

I naively assumed it was down to mobile safari since all the desktop devices we have are automatically time synched so they all appeared to work perfectly.

解决方案是从node.js内部全局设置开始时间.并计算每个从站系统时间的偏移量.此外,由于我们现在还针对每个设备的延迟进行测量和补偿,因此该方法已为所有设备实现了更好的同步.

The solution was to set the start time globally from within node.js. And calculate an offset versus each slaves system time. Also this method has achieved better synchronisation for all devices since we are also now measuring and offsetting for each devices latency.

结果在所有浏览器/设备上看起来都是完美且一致的.

The result appears perfect and consistent across all browsers/devices.

尽管显然我最初的问题是错误的,但如果有其他帮助,我将在此处保留此答案.

Although obviously my original question was erroneous, I will leave this answer here in case it helps anyone else.

这篇关于为什么移动浏览器似乎提供了与台式机浏览器不同的Javascript Date()结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆