class NumberTimeChange { constructor(options) { var data = { start: 0, end: 0, duration: 500, speed: 50, update: function (res) { }, complete: function (res) { } } this.options = Object.assign(data, options) this.loops = Math.ceil(this.options.duration / this.options.speed); this.unit = Math.ceil(this.options.end - this.options.start) this.unit = this.unit < 0 ? this.unit - 9 : this.unit this.unit = Math.ceil(this.unit / this.loops) this.interval = null this.init() } init() { if (this.options.start == this.options.end){ this.options.complete(this.options.start) return } if (this.unit == 0){ this.options.start = this.options.end this.options.complete(this.options.start) return } this.interval = setInterval(function () { this.main() }.bind(this), this.options.speed) } main() { var isEnd = this.unit >= 0 ? this.options.start >= this.options.end : this.options.start <= this.options.end this.options.start += this.unit if (isEnd) { clearInterval(this.interval) this.options.start = this.options.end this.options.complete(this.options.start) } else { this.options.update(this.options.start) } } } export default NumberTimeChange