mmik
mmik

Reputation: 5981

Seconds to minutes in VueJS

Unfortunately, the seconds to minute calculation using the parseInt() function isn't working with VueJS for me. I used the following to calculate minute from seconds:

parseInt(this.seconds / 60, 10) % 60

However, this.seconds value always returns 0 after 59 seconds, not 60 and as such the parseInt() function always returns 0. I am out of luck now. Below is the full code:

new Vue({
    el: '#app',
    data: {
        hours: 0,
        minutes: 0,
        seconds: 0
    },
    methods: {
        timer() {
            setInterval(() => {
                this.seconds = ++this.seconds % 60
                this.seconds = parseInt(this.seconds / 60, 10) % 60
            }, 1000);
        }
    }
})

Upvotes: 1

Views: 2614

Answers (2)

mmik
mmik

Reputation: 5981

I was able to fix this by myself. I was changing the original seconds value which was causing the issue to calculate the `minutes. So, I used a different variable to store the loop count and calculate minute:

new Vue({
    el: '#app',
    data: {
        hours: 0,
        minutes: 0,
        seconds: 0,
        counter : 0
    },
    methods: {
        timer() {
            setInterval(() => {
                this.seconds = ++this.counter % 60
                this.minutes = parseInt(this.counter / 60, 10) % 60
            }, 1000);
        }
    }
})

Upvotes: 2

ambs
ambs

Reputation: 487

You could maybe replace this.seconds with this shorthand expression?

this.seconds === 0 ? 60 : this.seconds

Upvotes: 0

Related Questions