开发者

How to get the server time in flex

I want to display the digital clock in my project which should dis开发者_StackOverflow社区play the server time.


you cannot get it with some AS class because flash is executed on client's computer. you need a script on the server that returns the time and call that to sync your own clock. you can do it only once, when the app is started and you can also time the moment when you made the request and when it came back and subtract that delay from the time so you won't be affected by the network lag.

and of course, you can ping that script now and then and resync if needed.


  • Upon initialization, load the time from the server using a URLLoader. The server script can pass current time in UTC or milliseconds passed since Linux epoch; add a second or two to the received value if you want to account for the delay in one-way trip from server to the Flex app
  • Read the local time (time in client machine) using Date class and store the difference between server time and local time in a variable.
  • Initialize your digital clock with server time.
  • Update the clock using a timer that fires every second or from the enterFrame event handler.
  • Instead of adding one second in every clock tick, read the local time and add the stored difference to it. This is because timer/enterFrame is not guaranteed to fire in exact intervals as it was specified to - it might get slower if your app is computationally heavy.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜