Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

[Vue.js] setInterval() runs only once, probably an issue with "this"?

Hey everyone! I'd like to create a clock in Vue.js, I call the functions in the "created:" block but it's only called once even though I set the setInterval to be called in every second. here's the code:

created: function() {
        setInterval(this.timer, 1000);
},

methods: {
                 timer: function () {
            let t = new Date();
            let h = this.zeroPadding(t.getHours(), 2);
            let m = this.zeroPadding(t.getMinutes(), 2);

            let s = t.getSeconds(); //this is not part of the original project, just a test to find the bug.
            console.log(s);

            this.time = h + " : " + m + " : " + s;
        },

                zeroPadding: function (num, digit) {
            let zero = '';
            for (let i = 0; i < digit; i++) {
                zero += '0';
            }
            return (zero + num).slice(-digit);
        },
},

I've also tried with: const self = this.

created: function() {
                const self = this;
        setInterval(self.timer, 1000);
},

methods: {

               timer: function () {
                        const self = this;
            let t = new Date();
            let h = self.zeroPadding(t.getHours(), 2);
            let m = self.zeroPadding(t.getMinutes(), 2);

            let s = t.getSeconds(); //this is not part of the original project, just a test to find the bug.
            console.log(s);

            self.time = h + " : " + m + " : " + s;
        },
                zeroPadding: function (num, digit) {
            let zero = '';
            for (let i = 0; i < digit; i++) {
                zero += '0';
            }
            return (zero + num).slice(-digit);
        },
},

The program runs once and I get the correct time and values but that's all it's like I did never declare a setInterval()
I have no idea what could be the problem, a simple setInterval(function() {console.log('hello')}) is working just fine. I guess it's an issue with the this key word but I don't know the solution.

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hello there! I dunno about Vue.js but Can you take a reference of another function which is defined in the local scope of another function? Also, If you wanna get the context of the current Object, you need to store the this reference in "self" variable, in order to proceed.

~ Ari

Problem solved