[ Vue筆記 ] Vue使用setInterval


Posted by Akira on 2022-02-20

前言

最近在寫番茄鐘的Side project,在使用 setInterval 做計時器時,遇到了小小問題。
這邊紀錄一下解法。

範例

  data() {
    return {
      time: null,
    };
  },
  methods: {
    timer() {
      this.time = setInterval(
        function () {
        // 想重複的動作
        }.bind(this),
        200
      );
    },
    setTime() {
      this.timer();
    },
    stopTime() {
      if (this.time) {
        clearInterval(this.time);
        this.time = null;
      }
    },
}









Related Posts

[03] v-slot ( slot, slot-scope )

[03] v-slot ( slot, slot-scope )

JS-[promises篇]-消耗一個promise以及串接promise 與錯誤處理

JS-[promises篇]-消耗一個promise以及串接promise 與錯誤處理

MTR04_0705

MTR04_0705


Comments