鍍金池/ 問(wèn)答/HTML/ 在mounted定義了一個(gè)定時(shí)器,要如何在destroyed中終止?

在mounted定義了一個(gè)定時(shí)器,要如何在destroyed中終止?

1、在mounted定義了一個(gè)定時(shí)器,要如何在destroyed中終止?
2、代碼如下:

  mounted: function () {
    this.$nextTick(function () {
      var shuffle = setInterval(() => {
        this.cells = _.shuffle(this.cells)
      }, 2500)
    })
  },
  destroyed: function () {
    clearInterval(shuffle);//該處的shuffle要如何指向mounted中的shuffle
  }
回答
編輯回答
編輯回答
糖豆豆

綁定到this上,然后進(jìn)行銷(xiāo)毀。

this.timer = setInterval(() => {
    ....
}, 2500)

// 銷(xiāo)毀
clearInterval(this.timer)
2017年10月2日 08:41