Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React Components (2018) Stateful Components and Lifecycle Methods Update the Stopwatch State with componentDidMount()

Herman Morales
Herman Morales
8,831 Points

Why did you use the this.intervalID instead a only a setInterval and where is the intervalID property? never wasdeclared

I don't understand why to use the keyworkd this with this.intervalID if never was declared interfalID before

Jorge Lopez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jorge Lopez
Python Development Techdegree Graduate 35,350 Points

Hi Herman! I know it's been 2 weeks, but here's something if you haven't found an answer yet.

I had the same question and saw yours. I found this article while looking for an answer. To summarize, setInterval() returns an intervalID every time it runs. You can use that ID to pass into other functions that will interact with that particular interval such as clearInterval() which will stop the interval whose ID is passed into it.

function timer () {
     let secsPassed = 0
     const intervalID = setInterval(timerFunction, 1000);

     function timerFunction() {
          if (secsPassed >= 10) {
               clearInterval(intervalID)
          } else {
               secsPassed++;
               console.log(secsPassed);
          }
     }
}

timer();

^^That code will console.log an incrementing number every second, and will stop once it reaches 10. The way it was stopped was by using the intervalID that setInterval() created and passing it into the clearInterval() function. If this is still confusing the article does a much better job at explaining it than I haha.