(function($) { $.fn.countto = function(opts) { var options = $.extend({}, $.fn.countto.defaults, opts); return $(this).each(function() { var _this = this, originaldata = $(this).text(), loops = math.ceil(options.speed / options.refreshinterval), increment = ($(this).text() - options.from) / loops, loopcount = 0, value = options.from, interval = setinterval(updatetimer, options.refreshinterval); function updatetimer() { value += increment; loopcount++; var str = value.tofixed(options.decimals); this.sizenum = str.length; this.sizenumbefore = this.sizenum - options.decimals - 1; if (this.sizenumbefore >= options.beforesize) { $(_this).html(str + options.lastsymbol); } else { this._str = array(options.beforesize - this.sizenumbefore + 1).join('0') + str; $(_this).html(this._str + options.lastsymbol); } if (typeof(options.onupdate) == 'function') { options.onupdate.call(_this, value, loopcount); } if (loopcount >= loops) { clearinterval(interval); $(_this).html(originaldata + options.lastsymbol); value = $(_this).text(); if (typeof(options.oncomplete) == 'function') { options.oncomplete(value, loopcount, _this); } } } }); }; $.fn.countto.defaults = { lastsymbol: "%", from: 0, speed: 1000, refreshinterval: 100, beforesize: 0, decimals: 0, onupdate: null, oncomplete: null }; })(jquery);