javascript - How to call setInterval method regularly in vue.js
巴扎黑
巴扎黑 2017-05-19 10:31:53
0
2
550
methods: {
    A: function() {
        setInterval(function(){ 
            this.B();
        },500)
    },
    B: function() {
        console.log('func B')
    }
}

Writing like this will report an error. How to achieve such an effect?

巴扎黑
巴扎黑

reply all(2)
淡淡烟草味

You can use arrow functions

methods: {
    A: function() {
        setInterval(() => { 
            this.B();
        }, 500)
    },
    B: function() {
        console.log('func B')
    }
}

or

methods: {
    A: function() {
        setInterval(this.B, 500)
    },
    B: function() {
        console.log('func B')
    }
}
阿神
methods: {
    A () {
        let that = this;
        setInterval(function(){ 
            that.B();
        },500)
    },
    B () {
        console.log('func B')
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template