Saturday 15 January 2011

jquery - How can I make my two JavaScripts work together? -



jquery - How can I make my two JavaScripts work together? -

hello trying have clock , countdown timer on same page people/users can see when done game , them have right time countdown timer also. here code. both work separately when both set on .php page bottom 1 (countdown timer) works , if jquery.noconflict timer clock works countdown doesn't.

<!-- clock part 1 - holder display of clock --> <span id="tp">&nbsp;</span> <!-- clock part 1 - ends here --> <!-- clock part 2 - set anywhere after part 1 --> <script type="text/javascript"> // clock script generated maxx blade's clock v2.0d // http://www.maxxblade.co.uk/clock function ts(){ x=new date(); x.settime(x.gettime()); homecoming x; } function lz(x){ homecoming (x>9)?x:'0'+x; } function th(x){ if(x==0){ x=12; } homecoming (x>12)?x-=12:x; } function de(x){ if(x==1||x==21||x==31){ homecoming 'st'; } if(x==2||x==22){ homecoming 'nd'; } if(x==3||x==23){ homecoming 'rd'; } homecoming 'th'; } function y2(x){ x=(x<500)?x+1900:x; homecoming string(x).substring(2,4) } function dt(){ window.status=''+eval(ot)+''; document.title=''+eval(ot)+''; document.getelementbyid('tp').innerhtml=eval(ot); settimeout('dt()',1000); } function ap(x){ homecoming (x>11)?'pm':'am'; } var dn=new array('sun','mon','tue','wed','thu','fri','sat'),mn=new array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'),ot="dn[ts().getday()]+' '+ts().getdate()+de(ts().getdate())+' '+mn[ts().getmonth()]+' '+y2(ts().getyear())+' '+':'+':'+' '+th(ts().gethours())+':'+lz(ts().getminutes())+':'+lz(ts().getseconds())+ap(ts().gethours())"; if(!document.all){ window.onload=dt; }else{ dt(); } </script> <!-- clock part 2 - ends here --> <script type="text/javascript"> //################################################################### // author: ricocheting.com // version: v3.0 // date: 2014-09-05 // description: displays amount of time until "datefuture" entered below. var cdown = function() { this.state=0;// if initialized this.counts=[];// array holding countdown date objects , id print {d:new date(2013,11,18,18,54,36), id:"countbox1"} this.interval=null;// setinterval object } cdown.prototype = { init: function(){ this.state=1; var self=this; this.interval=window.setinterval(function(){self.tick();}, 1000); }, add: function(date,id){ this.counts.push({d:date,id:id}); this.tick(); if(this.state==0) this.init(); }, expire: function(idxs){ for(var x in idxs) { this.display(this.counts[idxs[x]], "sorry, hopfully open in couple minutes"); this.counts.splice(idxs[x], 1); } }, format: function(r){ var out=""; if(r.d != 0){out += r.d +" "+((r.d==1)?"day":"days")+", ";} if(r.h != 0){out += r.h +" "+((r.h==1)?"hour":"hours")+", ";} out += r.m +" "+((r.m==1)?"min":"mins")+", "; out += r.s +" "+((r.s==1)?"sec":"secs")+", "; homecoming out.substr(0,out.length-2); }, math: function(work){ var y=w=d=h=m=s=ms=0; ms=(""+((work%1000)+1000)).substr(1,3); work=math.floor(work/1000);//kill "milliseconds" secs y=math.floor(work/31536000);//years (no leapyear support) w=math.floor(work/604800);//weeks d=math.floor(work/86400);//days work=work%86400; h=math.floor(work/3600);//hours work=work%3600; m=math.floor(work/60);//minutes work=work%60; s=math.floor(work);//seconds homecoming {y:y,w:w,d:d,h:h,m:m,s:s,ms:ms}; }, tick: function(){ var now=(new date()).gettime(), expired=[],cnt=0,amount=0; if(this.counts) for(var idx=0,n=this.counts.length; idx<n; ++idx){ cnt=this.counts[idx]; amount=cnt.d.gettime()-now;//calc milliseconds between dates // if time past if(amount<0){ expired.push(idx); } // date still else{ this.display(cnt, this.format(this.math(amount))); } } // deal expired if(expired.length>0) this.expire(expired); // if no active counts, stop updating if(this.counts.length==0) window.cleartimeout(this.interval); }, display: function(cnt,msg){ document.getelementbyid(cnt.id).innerhtml=msg; } }; window.onload=function(){ var cdown = new cdown(); //year,month,day,hour,min,sec\\ (jan - 0 fed - 1 ++ dec - 11, 12 replaced 0 jan) cdown.add(new date(2014,9,29,12,18,0), "countbox1"); }; </script> <h2> time until ^^<<>> opens!</h2> <div id="countbox1"></div>

fixed problem @scronide , @james thorpe , , @ultimater. set of methods place , kept them separate , used @scronide final method. again.

they're both assigning window.onload sec overwriting function of first.

if using jquery, instead of assigning window.onload, can instead use:

/* first component */ //initialisation first component $(function() { dt(); }); /* sec component */ //initialisation sec component $(function() { var cdown = new cdown(); //year,month,day,hour,min,sec\\ (jan - 0 fed - 1 ++ dec - 11, 12 replaced 0 jan) cdown.add(new date(2014,9,29,12,18,0), "countbox1"); });

javascript jquery

No comments:

Post a Comment