Skip to content

Instantly share code, notes, and snippets.

@penguinpowernz
Last active April 1, 2019 16:29
Show Gist options
  • Save penguinpowernz/96c266a2ad16bb8761b5 to your computer and use it in GitHub Desktop.
Save penguinpowernz/96c266a2ad16bb8761b5 to your computer and use it in GitHub Desktop.
Making a Circle Timer with Jquery Circle Progress

Making a Circle Timer with Jquery Circle Progress

I like the jquery-circle-progress plugin but I wanted it to do circle timing because I didn't like any of the other circle timers out there.

Here is what I came up with:

  <html>
    <body>
  
      <div id="circle_timer"></div>
  
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript" src="jquery-circle-progress.js"></script>
      <script type="text/javascript" src="jquery-circle-progress-timer.js"></script>
      
      <script type="text/javascript">
        circle_timer = new CircleTimer({
          target: "#circle_timer",
          duration: 60,
          onFinished: function() {
            console.log("timer finished!");
          }
        }, {
          fill: { color: "#1669a4" },
          animation: false,
          thickness: 40,
          size: 300
        });
        circle_timer.start();
      </script>
  
    </body>
  
  </html>

Pretty easy, the first block of options are the timer specific ones, and the second block of options go straight into the circleProgress class.

Here is what the above code would produce:

function CircleTimer(config, cp_config) {
this.init(config, cp_config);
}
CircleTimer.prototype = {
config: null,
cp_config: null,
cp_config_defaults: {
startAngle: 1.5 * Math.PI,
value: 0
},
duration: 60,
progressInterval: null,
$target: null,
timer: null,
init: function(config, cp_config) {
this.config = config;
this.$target = $(config["target"]);
if ( this.$target.size() == 0 ) { return false; }
// determine how much we need to move the progress meter
// every time depending on the duration needed
this.duration = config["duration"] || 60;
this.progressInterval = parseFloat(100 / this.duration / 100).toFixed(3);
this.cp_config = $.extend({}, this.cp_config_defaults, cp_config);
// setup the circle progress on the target
this.$target.circleProgress(this.cp_config);
this.timer = this.$target.data("circleProgress");
},
start: function() {
// set local variables because the setInterval function
// will execute in the scope of this function
var timer = this.timer;
var $target = this.$target;
var onFinished = this.config["onFinished"];
var progressInterval = parseFloat(this.progressInterval);
// start the timer
var timerIntervalId = setInterval(function() {
// calculate the next progress step
timer.value = parseFloat(parseFloat(timer.value + progressInterval).toFixed(3));
// check if the progress is complete (hitting 1 or more means done)
if ( timer.value >= 1 ) {
timer.value = 1; // set it to 1 so it looks perfectly complete
clearInterval(timerIntervalId); // stop the timer
// run the onFinished callback if set
if (typeof(onFinished) == "function") {
onFinished($target);
}
}
// redraw the circle with the new value
timer.draw();
}, 1000);
// set the timer interval on the elements data incase someone else
// wants to stop the timer
$target.data('timerIntervalId', timerIntervalId);
},
stop: function() {
clearInterval(this.$target.data('timerIntervalId')); // stop the timer
}
}
@dimitrilahaye
Copy link

Perfect, I was just searching for something like that ;)

@hadiljr
Copy link

hadiljr commented Dec 11, 2017

On line 24 of jquery-circle-progress-timer.js file:
if ( this.$target.size() == 0 ) { return false; }
The method SIZE of JQuery is deprecated. Use length insted:
if ( this.$target.length == 0 ) { return false; }

@psaruks
Copy link

psaruks commented Dec 26, 2017

Uncaught TypeError: Cannot read property 'value' of null
    at jquery-circle-progress-timer.js:50

why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment