亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Vuex:僅當存儲屬性存在時每 2 秒調度一個函數?

Vuex:僅當存儲屬性存在時每 2 秒調度一個函數?

慕娘9325324 2023-03-24 14:09:48
我正在嘗試運行一個函數,該函數發出 GET 請求以更新產品數量,但僅當商店currentProduct屬性存在時。如果沒有選定的產品,那么我不想讓間隔無緣無故地觸發一個功能。在偽代碼中,我基本上是說:如果 $this.store.getters['myStore/getCurrentProduct']那么 setInterval(this.$store.dispatch('updateQuantity'), 2000);我唯一的想法是為 currentProduct 創建一個計算屬性:computed: {     currentProd() {         return $this.store.getters['myStore/getCurrentProduct'];     }}然后看它:watch: {     currentProd(newVal,oldVal) {          if (newVal != null) {              let foo = setInterval(this.$store.dispatch('updateQuantity'), 2000);          }     } }我只是不確定如何防止它重疊并且有大量的間隔觸發
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

您應該將間隔對象存儲在一個地方,以便您可以輕松地重置它,而不是在每次觀察程序運行時在函數中創建一個新的本地間隔:


data () {

  return {

    productCheckInterval: null

  }

},

watch: {

  currentProd (newVal, oldVal) {

    clearInterval(this.productCheckInterval)

    if (newVal !== null) {

      this.productCheckInterval = setInterval(() => {

        this.$store.dispatch('updateQuantity')

      }, 2000)

    }

  } 

}


查看完整回答
反對 回復 2023-03-24
  • 1 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號