[Vuejs]-Strange behaviour from setInterval vuejs

0👍

Even though you are using Definite Assignment Assertions, and you don’t get any error, this.remaining is undefined, and you get NaN, at some point you need to assign it some value:

directly:

private  remaining: number = this.calculate();

calculate() {
   return (moment(this.end)
            .toDate()
            .getTime() -
             new Date().getTime()) /
             this.interval;
  }

refresh() {
   setInterval(() => this.remaining = this.calculate(), this.interval);
}

For some reason assign the first value directly inside setInterval not update the value. Will be interesting to kwon why :(.

Leave a comment