search

Home  >  Q&A  >  body text

javascript - How to call setInterval method regularly in vue.js

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?

巴扎黑巴扎黑2739 days ago552

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:33:53

    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')
        }
    }

    reply
    0
  • 阿神

    阿神2017-05-19 10:33:53

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

    reply
    0
  • Cancelreply