[Vuejs]-Countdown variable (HH:mm:ss)

1👍

You must use use timer and reactive data property. I recommend you to safe diff to components data, start timer on component mount and clear it beforeDestroy

data() {
    return {
        diff: this.calculareDiff() 
    }
}

methods: {
    calculareDiff() {
        const now = moment(new Date());
        const end = moment(this.date.expires);
        this.diff = moment.duration(end.diff(now));
    }
},

mounted() {
    this.timer = setInterval(() => this.calculareDiff(), 1000)
},

beforeDestroy() {
    clearInterval(this.timer)
}

Leave a comment