Created
February 2, 2012 18:22
-
-
Save impronunciable/1724963 to your computer and use it in GitHub Desktop.
vulsai counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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