-1

I develop vue 2 application and I need to run setInterval for the few times and stop it after some time. This works fine

setInterval(() => {
        console.log('function do this')
      }, 2000)

bit this doesn't work as expected. actually it doesn't runs even once.

const refreshIntervalId = setInterval(() => {
        console.log('function do this')
      }, 2000)

/* later */
setTimeout(clearInterval(refreshIntervalId), 10000)

How can I start setInterval? Thanks

Yaroslav Saenko
  • 517
  • 2
  • 7
  • 21

1 Answers1

2
setTimeout(clearInterval(refreshIntervalId), 10000)

This is running immediately. You need to wrap clearInterval in a function to prevent the interval from being cancelled.

setTimeout(() => clearInterval(refreshIntervalId), 10000)
Dan
  • 10,282
  • 2
  • 37
  • 64