Skip to content

Instantly share code, notes, and snippets.

@elin-moco
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elin-moco/6eab253e0dc12e78560f to your computer and use it in GitHub Desktop.
Save elin-moco/6eab253e0dc12e78560f to your computer and use it in GitHub Desktop.
Simple Integer Counter JQuery Plugin
$.fn.countInt = function(duration, from) {
from = typeof from !== 'undefined' ? from : 0;
duration = typeof duration !== 'undefined' ? duration : 1000;
var $this = $(this);
var to = $this.text();
$this.text(from);
$this.start = function() {
var stime = new Date().getTime();
function step() {
var now = new Date().getTime();
var progress = now - stime;
var oldValue = $this.text();
var value = (to - from) * progress / duration;
if (Math.abs(value - oldValue) >= 1) {
$this.text(Math.floor(value));
}
if (progress <= duration) {
requestAnimationFrame(step);
}
else {
requestAnimationFrame(function() {
$this.text(to);
});
}
}
requestAnimationFrame(step);
return $this;
};
return $this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment