[ 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

[JavaScript] JavaScript 入門

[JavaScript] JavaScript 入門

ASP.NET Core Web API 入門教學 - 使用DELETE刪除資料

ASP.NET Core Web API 入門教學 - 使用DELETE刪除資料

[Vue 學習筆記(二)] Vue class 和 style binding

[Vue 學習筆記(二)] Vue class 和 style binding


Comments