var countup = function(target, startval, endval, decimals, duration, options) { var lasttime = 0; var vendors = ['webkit', 'moz', 'ms', 'o']; for (var x = 0; x < vendors.length && !window.requestanimationframe; ++x) { window.requestanimationframe = window[vendors[x] + 'requestanimationframe']; window.cancelanimationframe = window[vendors[x] + 'cancelanimationframe'] || window[vendors[x] + 'cancelrequestanimationframe'] } if (!window.requestanimationframe) { window.requestanimationframe = function(callback, element) { var currtime = new date().gettime(); var timetocall = math.max(0, 16 - (currtime - lasttime)); var id = window.settimeout(function() { callback(currtime + timetocall) }, timetocall); lasttime = currtime + timetocall; return id } } if (!window.cancelanimationframe) { window.cancelanimationframe = function(id) { cleartimeout(id) } } this.options = { useeasing: true, usegrouping: true, separator: ',', decimal: '.' }; for (var key in options) { if (options.hasownproperty(key)) { this.options[key] = options[key] } } if (this.options.separator === '') this.options.usegrouping = false; if (!this.options.prefix) this.options.prefix = ''; if (!this.options.suffix) this.options.suffix = ''; this.d = (typeof target === 'string') ? document.getelementbyid(target) : target; this.startval = number(startval); this.endval = number(endval); this.countdown = (this.startval > this.endval); this.frameval = this.startval; this.decimals = math.max(0, decimals || 0); this.dec = math.pow(10, this.decimals); this.duration = number(duration) * 1000 || 2000; var self = this; this.version = function() { return '1.6.0' }; this.printvalue = function(value) { var result = (!isnan(value)) ? self.formatnumber(value) : '--'; if (self.d.tagname == 'input') { this.d.value = result } else if (self.d.tagname == 'text' || self.d.tagname == 'tspan') { this.d.textcontent = result } else { this.d.innerhtml = result } }; this.easeoutexpo = function(t, b, c, d) { return c * (-math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b }; this.count = function(timestamp) { if (!self.starttime) self.starttime = timestamp; self.timestamp = timestamp; var progress = timestamp - self.starttime; self.remaining = self.duration - progress; if (self.options.useeasing) { if (self.countdown) { self.frameval = self.startval - self.easeoutexpo(progress, 0, self.startval - self.endval, self.duration) } else { self.frameval = self.easeoutexpo(progress, self.startval, self.endval - self.startval, self.duration) } } else { if (self.countdown) { self.frameval = self.startval - ((self.startval - self.endval) * (progress / self.duration)) } else { self.frameval = self.startval + (self.endval - self.startval) * (progress / self.duration) } } if (self.countdown) { self.frameval = (self.frameval < self.endval) ? self.endval : self.frameval } else { self.frameval = (self.frameval > self.endval) ? self.endval : self.frameval } self.frameval = math.round(self.frameval * self.dec) / self.dec; self.printvalue(self.frameval); if (progress < self.duration) { self.raf = requestanimationframe(self.count) } else { if (self.callback) self.callback() } }; this.start = function(callback) { self.callback = callback; self.raf = requestanimationframe(self.count); return false }; this.pauseresume = function() { if (!self.paused) { self.paused = true; cancelanimationframe(self.raf) } else { self.paused = false; delete self.starttime; self.duration = self.remaining; self.startval = self.frameval; requestanimationframe(self.count) } }; this.reset = function() { self.paused = false; delete self.starttime; self.startval = startval; cancelanimationframe(self.raf); self.printvalue(self.startval) }; this.update = function(newendval) { cancelanimationframe(self.raf); self.paused = false; delete self.starttime; self.startval = self.frameval; self.endval = number(newendval); self.countdown = (self.startval > self.endval); self.raf = requestanimationframe(self.count) }; this.formatnumber = function(nstr) { nstr = nstr.tofixed(self.decimals); nstr += ''; var x, x1, x2, rgx; x = nstr.split('.'); x1 = x[0]; x2 = x.length > 1 ? self.options.decimal + x[1] : ''; rgx = /(\d+)(\d{3})/; if (self.options.usegrouping) { while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + self.options.separator + '$2') } } return self.options.prefix + x1 + x2 + self.options.suffix }; self.printvalue(self.startval) };