搜索

首页  >  问答  >  正文

javascript - vue.js怎么setInterval定时调用方法

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

这样写会报错,怎么实现这样的效果呢?

巴扎黑巴扎黑2839 天前589

全部回复(2)我来回复

  • 淡淡烟草味

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

    可以使用箭头函数

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

    或者

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

    回复
    0
  • 阿神

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

    雷雷

    回复
    0
  • 取消回复