Search code examples
vue.jsmomentjscountdowntimer

Vue countdown timer prints out on console.log but don't get passed on as data


I am using moment js to countdown the time as 10,9,8,7,6 . It successfully counts down time on console.log but I am not able to pass that as a data.

Below is my code on jsfiddle

https://jsfiddle.net/ujjumaki/817bqn52/9/


Solution

  • Change the function inside setInterval to an arrow functions, in order to read this as the Vue component:

    setInterval(() => {
          duration.subtract(interval, "milliseconds"); //using momentjs substract function
          this.countdownTimer = moment(duration.asMilliseconds()).format('s'); 
          console.log(moment(duration.asMilliseconds()).format('s'));
          /* countdown timer works for console.log */
    }, interval );