LoginSignup
0
0

More than 3 years have passed since last update.

【JavaScript】clearIntervalメソッドの使い方

Last updated at Posted at 2021-03-02

clearInterval()

setInterval()でセットしたタイマーを解除する

これが元になって調査

methods: {
    startTimer: function () {
        // 残り5秒
        this.restSec = 5;
        // タイマースタート。1秒(1000ミリ秒)ごとに、1秒減らす
        this.timerObj = setInterval(() => {
            this.restSec--;
        }, 1000)
        // console.log(this.timerObj);
    }
},
watch: {
        restSec: function() {
            // 0秒以下になったらアラート&タイマー停止
            if (this.restSec <= 0) {
                alert("制限時間です。");
                clearInterval(this.timerObj);
            }
        },

}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0