I created a function that creates a certain clock. The goal is to use the function to reuse multiple times by changing the parameters of x
and y
. I use the setInterval
function to update every second. The problem is that the function is only being used once.
Complete code: link
var cont = 0;
function horloge_dynamique(x,y,diametre,decalage){
var svg = document.getElementById('svg');
cont++;
//cree circle
var newCircle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
newCircle.setAttributeNS(null, 'id', 'circle' + cont);
newCircle.setAttributeNS(null,'stroke-width',2);
newCircle.setAttributeNS(null,'stroke','black');
newCircle.setAttributeNS(null,'fill','Aqua');
svg.appendChild(newCircle);
//cree aiguille heure
var newLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
newLine.setAttributeNS(null, 'id', 'heure' + cont);
newLine.setAttributeNS(null, "stroke","red");
newLine.setAttributeNS(null, "stroke-width","4");
svg.appendChild(newLine);
//cree aiguille minute
var newLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
newLine.setAttributeNS(null, 'id', 'min' + cont);
newLine.setAttributeNS(null, "stroke","orange");
newLine.setAttributeNS(null, "stroke-width","2");
svg.appendChild(newLine);
//cree aiguille seconde
var newLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
newLine.setAttributeNS(null, 'id', 'sec' + cont);
newLine.setAttributeNS(null, "stroke","black");
newLine.setAttributeNS(null, "stroke-width","1");
svg.appendChild(newLine);
//var circle = document.getElementById("circle_horloge");
var circle = document.getElementById("circle" + cont);
circle.setAttribute("cx",x);
circle.setAttribute("cy",y);
circle.setAttribute("r",diametre);
//heure
//var aiguille_heure = document.getElementById("heure");
var aiguille_heure = document.getElementById("heure"+ cont);
aiguille_heure.setAttribute("x1",x);
aiguille_heure.setAttribute("y1",y);
aiguille_heure.setAttribute("x2",x);
aiguille_heure.setAttribute("y2",(y-diametre)+diametre/3);
//minutes
var aiguille_min = document.getElementById("min"+ cont);
aiguille_min.setAttribute("x1",x);
aiguille_min.setAttribute("y1",y);
aiguille_min.setAttribute("x2",x);
aiguille_min.setAttribute("y2",(y-diametre)+diametre/5);
//secondes
var aiguille_sec = document.getElementById("sec"+cont);
aiguille_sec.setAttribute("x1",x);
aiguille_sec.setAttribute("y1",y);
aiguille_sec.setAttribute("x2",x);
aiguille_sec.setAttribute("y2",y-diametre);
setInterval(function(){
var d = new Date(); //date
var heure_decalage = d.getHours()+decalage;
var minute = d.getMinutes();
var seconds = d.getSeconds();
var heure_calc = (30*((heure_decalage)%12) + (d.getMinutes()/2));
var minute_calc = 6*minute;
var seconde_calc = 6*seconds;
var heure = document.getElementById("heure"+ cont);
var min = document.getElementById("min"+ cont);
var sec = document.getElementById("sec"+ cont);
//title horloge
document.getElementById("circle"+cont).setAttribute("title", heure_decalage + ":" + minute);
heure.setAttribute('transform', 'rotate('+ heure_calc +" "+ x +" "+ y +')');
min.setAttribute('transform', 'rotate('+ minute_calc +" "+ x +" "+ y +')');
sec.setAttribute('transform', 'rotate('+ seconde_calc +" "+ x +" "+ y +')');
//setTimeout("r(x,y,diametre,decalage,cont)", 1000);
}, 1000)
}
var x = 500;
var y = 250;
var x1 = 200;
var y1 = 100;
var diametre = 100;
var decalage = 0;
horloge_dynamique(200,200,diametre,decalage);
horloge_dynamique(100,100,diametre,decalage);
<svg id="svg" width="100%" height="800">
</svg>