Skip to content

Instantly share code, notes, and snippets.

@impronunciable
Created February 2, 2012 18:22
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 impronunciable/1724963 to your computer and use it in GitHub Desktop.
Save impronunciable/1724963 to your computer and use it in GitHub Desktop.
vulsai counter
;(function ( $, window, document, undefined ) {
var defaults = {
start: 0
, update_interval: 2000
, stop: false
, stop_at: 600000
, min_count: 1
, max_count: 5
};
/*
* Constructor
*/
function vulsaiCounter(element, options){
this.options = $.extend({}, defaults, options);
this.el = $(element);
this.counter = this.options.start;
this.init();
};
vulsaiCounter.prototype.init = function(){
var self = this;
this.draw();
this.counter_interval = setInterval(function(){
self.update()
}, self.options.update_interval);
if(this.options.stop === true){
setTimeout(function(){
clearInterval(self.counter_interval);
}, self.options.stop_at);
}
};
vulsaiCounter.prototype.draw = function(){
var arr = util.num_split(this.counter, this.el.children('span').length);
this.el.children('span').each(function(i, el){
$(this).text(arr[i]);
});
};
vulsaiCounter.prototype.update = function(){
var self = this;
this.counter += self.options.min_count + Math.round(Math.random()*(self.options.max_count-self.options.min_count));
this.draw();
};
var util = {};
util.num_split = function(num, counter_length){
var arr = [];
while(num > 0){
arr.push(num % 10);
num = Math.floor(num / 10);
}
while(arr.length < counter_length){
arr.push(0);
}
return arr.reverse();
};
$.fn.vulsaiCounter = function ( options ) {
return this.each(function () {
if(!$.data(this, 'plugin_vulsaiCounter')) {
$.data(this, 'plugin_vulsaiCounter', new vulsaiCounter(this, options));
}
});
};
})(jQuery, window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment