Je suis en train d'essayer de faire une classe pour créer une horloge (et en avoir plusieurs sur la même page) mais je me heurte à un problème ... En effet, je n'arrive pas à utiliser la fonction setTimeout() pour appeler une méthode.
Voici le code :
Je suis débutant complet avec les classes en JavaScript mais là tout fonctionne sauf le rafraîchissement des horloges, ce qui m'amène à venir demander votre aide.
Spartakis
Modifié par Spartakis (25 Nov 2006 - 13:17)
Voici le code :
function test(num) {
//Variables à modifier :
//Les couleurs :
this.fCol='7777BB'; //couleur des chiffres.
this.sCol='9999FF'; //couleur des secondes.
this.mCol='666699'; //couleur des minutes.
this.hCol='000066'; //couleur des heures.
//position en absolut :
this.Xoff=100*(num-0.5); //à gauche
this.Yoff=47; //en haut
//taille
this.Ybase=34; //hauteur
this.Xbase=34; //largeur
//ne pas modifier ci-après :
this.H='...';
this.H=this.H.split('');
this.M='....';
this.M=this.M.split('');
this.S='.....';
this.S=this.S.split('');
this.NS4=(document.layers);
this.NS6=(document.getElementById&&!document.all);
this.IE4=(document.all);
this.Ypos=0;
this.Xpos=0;
this.dots=12;
this.Split=360/this.dots;
if(this.NS6) {
for(i=1; i < this.dots+1; i++) {
with(this) document.write('<div id="n6Digits'+i+num+'" style="position:absolute;top:0px;left:0px;width:30px;height:30px;font-family:Arial;font-size:10px;color:#'+fCol+';text-align:center;padding-top:10px">'+i+'</div>');
}
for(i=0; i < this.M.length; i++) {
with(this) document.write('<div id="Ny'+i+num+'" style="position:absolute;top:0px;left:0px;width:2px;height:2px;font-size:2px;background:#'+mCol+'"></div>');
}
for(i=0; i < this.H.length; i++) {
with(this) document.write('<div id="Nz'+i+num+'" style="position:absolute;top:0px;left:0px;width:2px;height:2px;font-size:2px;background:#'+hCol+'"></div>');
}
for(i=0; i < this.S.length; i++) {
with(this) document.write('<div id="Nx'+i+num+'" style="position:absolute;top:0px;left:0px;width:2px;height:2px;font-size:2px;background:#'+sCol+'"></div>');
}
}
}
function clock(num,fuseau){
this.time = new Date ();
this.secs = this.time.getSeconds();
this.sec = -1.57 + Math.PI * this.secs/30;
this.mins = this.time.getMinutes();
this.min = -1.57 + Math.PI * this.mins/30;
this.hr = this.time.getHours();
this.hr = this.hr+fuseau;
this.hrs = -1.57 + Math.PI * this.hr/6 + Math.PI*parseInt(this.time.getMinutes())/360;
if(this.NS6) {
this.Ypos=this.Yoff;
this.Xpos=this.Xoff;
for(i=1; i < this.dots+1; i++) {
with(this) document.getElementById("n6Digits"+i+num).style.top=Ypos-15+Ybase*Math.sin(-1.56 +i *Split*Math.PI/180)
with(this) document.getElementById("n6Digits"+i+num).style.left=Xpos-15+Xbase*Math.cos(-1.56 +i *Split*Math.PI/180)
}
for(i=0; i < this.S.length; i++) {
with(this) document.getElementById("Nx"+i+num).style.top=Ypos+i*Ybase/4.1*Math.sin(sec);
with(this) document.getElementById("Nx"+i+num).style.left=Xpos+i*Xbase/4.1*Math.cos(sec);
}
for(i=0; i < this.M.length; i++) {
with(this) document.getElementById("Ny"+i+num).style.top=Ypos+i*Ybase/4.1*Math.sin(min);
with(this) document.getElementById("Ny"+i+num).style.left=Xpos+i*Xbase/4.1*Math.cos(min);
}
for(i=0; i < this.H.length; i++) {
with(this) document.getElementById("Nz"+i+num).style.top=Ypos+i*Ybase/4.1*Math.sin(hrs);
with(this) document.getElementById("Nz"+i+num).style.left=Xpos+i*Xbase/4.1*Math.cos(hrs);
}
}
window.setTimeout('this.clock', 500);
}
test.prototype.clock=clock;
clock1=new test(1);
clock2=new test(2);
clock3=new test(3);
clock4=new test(4);
clock1.clock(1, +1);
clock2.clock(2, -5);
clock3.clock(3, +0);
clock4.clock(4, -1);
Je suis débutant complet avec les classes en JavaScript mais là tout fonctionne sauf le rafraîchissement des horloges, ce qui m'amène à venir demander votre aide.
Spartakis
Modifié par Spartakis (25 Nov 2006 - 13:17)