Click to See Complete Forum and Search --> : using server time in countdown clock rather than client time


cgiwizard
09-03-2003, 09:38 AM
hi!

this is a semi perl/js question but i thought i would try here first :)

I have a dynamic countdown script that counts down to a particular date/time event. What it is doing though is that it's taking a specified time from the perl script and counting down against the time which is gather from the users pc's - as alot of ppl's pc clocks can be set to the wrong time the countdown is different for every user.

what i would like to do is get the script to take the specified time from the perl script then count down against the time on the server and not the time on the user's pc

is there a function in js that will grab the time at the server?

heres the function which handle the timeupdate if its any help...

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Steve Dondley (steve\@dondley.com) -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
function getTime(future) {
lasttime = future;
// now=new Date('$srvtime');
now=new Date;
y2k = new Date(future);
days = (y2k - now) / 1000 / 60 / 60 / 24;
daysRound = Math.floor(days);
hours = (y2k - now) / 1000 / 60 / 60 - (24 \* daysRound);
hoursRound = Math.floor(hours);
minutes = (y2k - now) / 1000 /60 - (24 \* 60 \* daysRound) - (60 \* hoursRound);
minutesRound = Math.floor(minutes);
seconds = (y2k - now) / 1000 - (24 \* 60 \* 60 \* daysRound) - (60 \* 60 \* hoursRound) - (60 \* minutesRound);
secondsRound = Math.round(seconds);
sec = (secondsRound == 1) ? " second." : " seconds.";
min = (minutesRound == 1) ? " minute " : " minutes, ";
hr = (hoursRound == 1) ? " hour " : " hours, ";
dy = (daysRound == 1) ? " day " : " days, "
document.timeForm.input1.value = "" + daysRound + dy + hoursRound + hr + minutesRound + min + secondsRound + sec;
newtime = window.setTimeout("getTime(lasttime);", 1000);
}
</script>

any help greatly appreciated on this one :) :)
TIA
Jimbo

Khalid Ali
09-03-2003, 11:08 AM
IMHO

this will bring down your server(potentially)
imaging if several visitors are linked to your server and for every second of comparison client browser is sending a request to your server to send the time...????
make any sense???
the best I can think of is pass the time value from your server to the client and set a date object with that value.
then using that data object do some calculation and count down stuff