diff --git a/src/components/text-counter.js b/src/components/text-counter.js index 5259448..e777448 100644 --- a/src/components/text-counter.js +++ b/src/components/text-counter.js @@ -14,45 +14,33 @@ AFRAME.registerComponent('text-counter', { }, init: function () { - this.startTime = null; this.currentValue = 0; this.textValue = {value : ''}; }, - decimals: function (n) { - var d = Math.pow(10, this.data.decimals); - return (parseInt(n * d) / d).toFixed(this.data.decimals); - }, - update: function (oldData) { - this.startTime = null; this.currentValue = 0; this.textValue.value = `${this.data.prefix} ${this.decimals(0)} ${this.data.suffix}`; this.el.setAttribute('text', this.textValue); }, - tick: function (time) { + tick: function (time, timeDelta) { if (this.currentValue >= this.data.value) { return; } - if (this.startTime === null) { - this.startTime = time; - return; - }; - - const prevValue = Math.floor(this.currentValue); - this.currentValue = this.data.value * (time - this.startTime) / this.data.dur; - - if (Math.floor(this.currentValue) === prevValue) { return; } + this.currentValue += this.data.value * (timeDelta / this.data.dur); if (this.currentValue >= this.data.value) { this.currentValue = this.data.value; - if (this.data.emit) { - this.el.emit('textcounterdone', null, false); - } + if (this.data.emit) { this.el.emit('textcounterdone', null, false); } } this.textValue.value = `${this.data.prefix} ${this.decimals(this.currentValue)} ${this.data.suffix}`; this.el.setAttribute('text', this.textValue); + }, + + decimals: function (n) { + var d = Math.pow(10, this.data.decimals); + return (parseInt(n * d) / d).toFixed(this.data.decimals); } })