Why does mobile safari seem to give different Javascript Date() results from desktop browsers?

删除回忆录丶 提交于 2019-12-08 04:36:00

问题


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.

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;
},

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

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 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.

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.

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.

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.

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.

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.

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.

EDIT: 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.


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/17423557/why-does-mobile-safari-seem-to-give-different-javascript-date-results-from-des

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!