I want to display the digital clock in my project which should display the server time.
+1
A:
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.
TheBrain
2010-06-10 06:30:12
A:
- 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.
Amarghosh
2010-06-10 06:35:28