I’m having a working countdown timer the only thing is i need to show it multiple times and I can’t seem to get it working for multiple counters.
The working code : http://jsfiddle.net/3RpvJ/
I have tried to make more div’s like timer1 timer2 enz. but i cant seem to get it working
window.onload = function() {
canvas = document.getElementById('timer'),
seconds = document.getElementById('counter'),
ctx = canvas.getContext('2d'),
sec = 180,
countdown = sec;
ctx.lineWidth = 8;
ctx.strokeStyle = "#528f20";
var
startAngle = 0,
time = 0,
intv = setInterval(function(){
var endAngle = (Math.PI * time * 2 / sec);
ctx.arc(65, 35, 30, startAngle , endAngle, false);
ctx.clearRect(0, 0, 200, 200);
startAngle = endAngle;
ctx.stroke();
countdown--;
if ( countdown > 60){
seconds.innerHTML = Math.floor(countdown/60);
seconds.innerHTML += ":" + countdown%60;
}
else{
seconds.innerHTML = countdown;
}
if (++time > sec,countdown == 0 )
{
clearInterval(intv),
$("#timer, #counter").remove(),
$("#timers").prepend('<img id="theImg" src="http://ivojonkers.com/votify/upvote.png" />');
}
}, 10);
}
http://jsfiddle.net/3RpvJ/5/
html:
CSS:
I created a Timer function that will take a duration, a parent element, as well as optional selectors for the canvas and counter. If
canandcntaren’t specified, and don’t exist, it will create them. To the html, I added an extra grouping div so that all of the elements for each timer have a common parent. For the CSS, I changed your naming convention to keep it a bit more dry. Each Timer parent div will have the class “time”, but that can be whatever you want. The Timer will assume that the canvas has a class of “timer” and the time output will have a class of “counter”. These can also be whatever you want, but you’ll have to specify the parameters in that case.