Quick Javascript Help

Soldato
Joined
21 Jul 2004
Posts
6,360
Location
Harrow, UK
I am trying to run code for two clocks on the same HTML page, and because it uses the same variables etc the first clock does not work :(

Code:
<!-- Clock 1-->
<script language="JavaScript">

function tS(){ x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()+dS()+0); return x; } 
function tN(){ return new Date(); } 
function dS(){ return ((tN().getTime()>fD(0,2,3,-1).getTime())&&(tN().getTime()<fD(0,9,2,-1).getTime()))?3600000:0; } 
function fD(d,m,h,p){ var week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date(tN().getUTCFullYear(),nm,1,h,0,0),dOff=0; if(p<0){ x.setTime(x.getTime()-86400000); } if(x.getDay()!=d){ dOff=(x.getDay()<d)?(d-x.getDay()):0-(x.getDay()-d); if(p<0&&dOff>0){ week-=7; } if(p>0&&dOff<0){ week+=7; } x.setTime(x.getTime()+((dOff+week)*86400000)); } return x; } 
function lZ(x){ return (x>9)?x:'0'+x; } 
function dT(){ if(fr==0){ fr=1; document.write('<font size=4 face=Verdana font color="#666666"><span id="tP">'+eval(oT)+'</span>BST</font>'); } document.getElementById('tP').innerHTML=eval(oT); setTimeout('dT()',1000); } 
var fr=0,oT="lZ(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+' '";
</script>

<script language="JavaScript">dT();</script>
<!-- End of Clock 1-->

<br>

<!-- Clock 2-->
<script language="JavaScript">

function tS(){ x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()-18000000); return x; } 
function tN(){ return new Date(); } 
function lZ(x){ return (x>9)?x:'0'+x; } 
function dT(){ window.status=''+eval(oT)+''; if(fr==0){ fr=1; document.write('<font size=1 face=Verdana  color="#666666">CN Time&nbsp;<span id="tP">'+eval(oT)+'</span>&nbsp;</font>'); } document.getElementById('tP').innerHTML=eval(oT); setTimeout('dT()',1000); } 
var fr=0,oT="lZ(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+' '";
</script>

<script language="JavaScript">dT();</script>
<!-- End of Clock 2-->
 
Sorry Una, the code wasn't actually written by me - it was just found on a website with lots of different clock codes. I just didn't know how to make it work...

Works brilliantly now though :D
Cheers! :)

/Edit: Hmm, I tried doing exactly what you have done, but it didn't work for some strange reason :confused:
 
Back
Top Bottom