Permalink
Browse files

adding capabilities to allow timer to loop, to run at start (without …

…timer selection) and to run with single interval (rather than multiple with selection)
  • Loading branch information...
1 parent 29263f4 commit bdc6fdf7b539b0eea393dd0c54b0cc9cd02caac7 @plukevdh plukevdh committed Feb 25, 2010
Showing with 46 additions and 20 deletions.
  1. +46 −20 lib/jquery.timer360.js
@@ -33,7 +33,9 @@
strokeColor : "#477050", // the color of the stroke
fillColor : "#8ac575", // the fill color
intervals : [5,25,30], // the options for the timer
+ interval : 10, // allowed to have single timer.
seconds : false, // if false then interval is considered minutes
+ loop : false, // timer will loop if true
onComplete : new Function
}, options);
@@ -44,32 +46,41 @@
return this.each(function () {
var $this = $(this);
- var interval = null;
+ var interval = settings.interval;
var start = null;
// Create the canvas
var $canvas = $('<canvas id="360timer_' + $this.attr("id") + '" width="' +
settings.width + '" height="' +
settings.height + '"></canvas>');
var canvas = $canvas[0];
- var intervalItems = '';
- for (x in settings.intervals)
- {
- intervalItems += '<li>' + settings.intervals[x] + '</li>'
- }
- var $timerSelect = $('<div class="timer_select">' +
- '<ul>' + intervalItems + '</ul>' + '</div>');
-
- $this.prepend(canvas).append($timerSelect.hide()).css({position:'relative'}).click(function () {
- $timerSelect.toggle();
- });
-
- $timerSelect.find('li').click(function () {
- if (interval) { clearInterval(interval) }
- start = new Date();
- tick(settings.seconds ? $(this).text()/60 : $(this).text());
- });
-
+
+ $this.prepend(canvas);
+
+ if(settings.intervals != false) {
+ var intervalItems = '';
+
+ for (x in settings.intervals) {
+ intervalItems += '<li>' + settings.intervals[x] + '</li>'
+ }
+
+ var $timerSelect = $('<div class="timer_select">' +
+ '<ul>' + intervalItems + '</ul>' + '</div>');
+
+ $this.append($timerSelect.hide()).css({position:'relative'}).click(function () {
+ $timerSelect.toggle();
+ });
+
+ $timerSelect.find('li').click(function () {
+ if (interval) { clearInterval(interval) }
+ start = start_clock($(this));
+ });
+ }
+
+ if(settings.intervals == false && start == null) {
+ start = start_clock(null);
+ }
+
// get context for the canvas and create the initial circle
var pen = canvas.getContext('2d');
pen.lineWidth = settings.strokeWidth;
@@ -91,12 +102,16 @@
pen.clearRect(0,0,settings.width, settings.height);
drawTimer(Math.PI*2, false);
- if ((secondsElapsed - 1) < (amount*60)) {
+ if ((secondsElapsed) < (amount*60)) {
drawTimer(incrementAmount*(secondsElapsed*1000), true);
} else {
drawTimer(Math.PI*2, true)
clearInterval(interval);
settings.onComplete();
+ if(settings.loop == true) {
+ clearInterval(interval)
+ start = start_clock(null);
+ }
}
}, 1000);
}
@@ -107,6 +122,17 @@
pen.fill();
if (drawStroke) { pen.stroke(); }
}
+
+ function start_clock(clock) {
+ var start = new Date();
+ if(settings.intervals != false && clock != null) {
+ tick(settings.seconds ? clock.text()/60 : clock.text());
+ } else {
+ tick(settings.seconds ? settings.interval/60 : settings.interval);
+ }
+ return start;
+ }
+
});
};

0 comments on commit bdc6fdf

Please sign in to comment.