/    Sign up×
Community /Pin to ProfileBookmark

digital clock with server time

[B]Dear all[/B],
following code snippet is about the digital clock where i am passing the server time in variable “time_string” , now i want to make this work , This time will be fetched everytime same so how can i assign this time to new Date(); so everytime it will update it …

[code]
var time_str = document.clock_form.time_str.value ; //Sat, 05 Jun 2010 16:48:00 +0530
function dotime(){
theTime=setTimeout(‘dotime();’,1000);
d= new Date(Date.parse(time_str));
var hr= d.getHours()+100;
var mn= d.getMinutes()+100;
var se= d.getSeconds()+100;

if(hr==100){
hr=112;am_pm=’am’;
}
else if(hr<112){
am_pm=’am’;
}
else if(hr==112){
am_pm=’pm’;
}
else if(hr>112){
am_pm=’pm’;hr=(hr-12);
}
tot=”+hr+mn+se;
document.hr1.src = ‘/flash_files/digits/dg’+tot.substring(1,2)+’.gif’;
document.hr2.src = ‘/flash_files/digits/dg’+tot.substring(2,3)+’.gif’;
document.mn1.src = ‘/flash_files/digits/dg’+tot.substring(4,5)+’.gif’;
document.mn2.src = ‘/flash_files/digits/dg’+tot.substring(5,6)+’.gif’;
document.se1.src = ‘/flash_files/digits/dg’+tot.substring(7,8)+’.gif’;
document.se2.src = ‘/flash_files/digits/dg’+tot.substring(8,9)+’.gif’;
document.ampm.src= ‘/flash_files/digits/dg’+am_pm+’.gif’;
}
dotime();
[/code]

Only once it work and give error , Plz help me out

Thanks in advance

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorJun 05.2010 — <i>
</i>onload=function(){
dotime();
}

ant put the timeout better at the end of the function. Or use a setInterval() instead of setTimeout() an use it once, in [B]onload[/B].
Copy linkTweet thisAlerts:
@gskoliauthorJun 05.2010 — thanks for reply ,

but how it will update time , it is not doing that .
Copy linkTweet thisAlerts:
@Sterling_IsfineJun 05.2010 — thanks for reply ,

but how it will update time , it is not doing that .[/QUOTE]


Of course not. You need to record the client time when the page loads, then each time you run your function read the client time, calculate the elapsed time and add it to the initial server time that you read when the page loaded.

No.
Copy linkTweet thisAlerts:
@SparoHawkJun 05.2010 — Well, I don't think its a good idea to rely on getting the time from the server for every second. Depending on the connection speed your AJAX calls can last from a few milliseconds to some long seconds.

Just get the initial time from the server when the page loads and keep the clock ticking using JavaScript with a setInterval of 1 second.
Copy linkTweet thisAlerts:
@criterion9Jun 05.2010 — I don't think the suggestion was to get new time from the server every second. The suggestion looked to me like the time is sent from the server during page load and the javascript appends elapsed time to that value every second.
Copy linkTweet thisAlerts:
@SparoHawkJun 05.2010 — Ah, got that right [B]criterion9[/B]. Thanks for pointing it out.

Then do as [B]Kor[/B] points out then.
Copy linkTweet thisAlerts:
@KorJun 06.2010 — simply set the server time onload. You may perform an AJAX call from time to time to adjust the difference which might occur meanwhile. It the depends on the accuracy you need. Once in 5-10 minutes or so, should be enough.
Copy linkTweet thisAlerts:
@gskoliauthorJun 07.2010 — Thanks guys for replying ,

One suggestion is i am going to follow for test purpose that from server time and local time take difference and go ahead with that ....

Lets see ... If found difficulty will let you know ....

Thanks again ...
×

Success!

Help @gskoli spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...