Skip to main content
Inspiring
January 19, 2011
Answered

Server time

  • January 19, 2011
  • 2 replies
  • 1934 views

I'm trying to create a clock which shows the time returned by the server. Help me to get this code working with the server time on..

(E.g.: time.php returns 1295532770)

server = new LoadVars();

server.onData = function (src){

clock_txt.onEnterFrame = time;

this.time = Number(src);

};

server.load("http://www.me.com/time.php");

var seconds = this.time/1000

var minutes = this.time/60

var hours = this.time/60

if (hours<12) {

ampm = "AM";

}

else{

ampm = "PM";

}

while(hours >12){

hours = hours - 12;

}

if(hours<10)

{

hours = "0" + hours;

}

if(minutes<10)

{

minutes = "0" + minutes;

}

if(seconds<10)

   {

seconds = "0" + seconds;

}

clock_txt.text = hours + ":" + minutes + ":" + seconds +" "+ ampm;

This topic has been closed for replies.
Correct answer kglad

oh ok. But still the time doesn't show up.

function timeF(){

trace("timeMS "+tl.timeMS);

var d:Date = new Date(tl.timeMS+getTimer()-startTime);

OUTPUT

timeMS 1295694898

hours 5

hourS undefined

_level0.clock_txt 05:24:54 am

timeMS 1295694898

hours 5

hourS undefined

_level0.clock_txt 05:24:54 am

timeMS 1295694898

hours 5

hourS undefined

_level0.clock_txt 05:24:55 am

timeMS 1295694898

hours 5

hourS undefined

_level0.clock_txt 05:24:55 am

timeMS 1295694898

hours 5

hourS undefined

_level0.clock_txt 05:24:55 am


remove all the trace() statements.

the code is working as expected.  clock_txt.text is correct.

2 replies

kglad
Community Expert
Community Expert
January 20, 2011

then use:

var tl:MovieClip=this;

var startTime:Number;

server = new LoadVars();

server.onData = function (src){

tl.timeMS = Number(src);

startTime=getTimer();

clock_txt.onEnterFrame = timeF;

};

server.load("http://www.me.com/time.php");

function timeF(){

var d:Date = new Date(tl.timeMS+getTimer()-startTime);
if(d.getHours()>12){
var hours:Number=d.getHours()-12;
var ampm:String = "pm";
} else if(d.getHours()==12){
hours=12;
ampm="pm"
} else {
hours=d.getHours();
ampm="am";
}

var hoursS:String = formatS(hours.toString());
var minutes:String = formatS(d.getMinutes().toString());
var seconds:String = formatS(d.getSeconds().toString());

clock_txt.text = hours + ":" + minutes + ":" + seconds +" "+ ampm;

}

function formatS(s:String):String{

while(s.length<2){

s="0"+s;

}

return s;

}

Inspiring
January 20, 2011

Thanks Kglad but nothing seems to appear.

kglad
Community Expert
Community Expert
January 20, 2011

then you don't have a textfield with visible text or you onData() method isn't being called.  use the trace() function or a textfield to see which it is.

kglad
Community Expert
Community Expert
January 19, 2011

you have some problems with logic but to start, there's nothing returned from the php file.  use the trace function or a textfield to confirm.

Inspiring
January 20, 2011

Thanks.

server = new LoadVars();

server.onData = function (src){

clock_txt.onEnterFrame = time;

this.time = Number(src);

trace (this.time);

};

outputs

1295554175

1295554175

1295554176

1295554176

1295554176

1295554176

....