Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
Full Stack JavaScript Techdegree Graduate 24,871 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.