搜尋

首頁  >  問答  >  主體

javascript - vue.js怎麼setInterval定時呼叫方法

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

這樣寫會報錯,要怎麼達到這樣的效果呢?

巴扎黑巴扎黑2839 天前586

全部回覆(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
  • 取消回覆