1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

setIntervalで実行する関数内にintervalIdを渡してclearIntervalしたい

Posted at

setIntervalは戻り値にintervalIdを返し、これをclearIntervalに渡すことで繰り返し処理を停止する事ができる。

intervalIdをグローバルな変数として定義しておくことで可能。


javascript:
  let intervalId = 0
  let ii = 0
  const getRequestedSharing = () => {
    console.log('getRequestedSharing', intervalId, ii++)
    if (ii > 5) {
      clearInterval(intervalId)
    }

  }
  document.addEventListener('turbolinks:load', () => {
    intervalId = window.setInterval(getRequestedSharing, 3000)  //3000ミリ秒ごとに実行
  })

参考

JavaScript - clearIntervalを実行したときの挙動|teratail

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?