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 Stateful Components and Lifecycle Methods Resetting the Stopwatch

Jose Morales-Mendizabal
Jose Morales-Mendizabal
18,754 Points

Why not call Tick() every 1000ms instead of doing the Math?

I'm not quite sure why Guil is calling the Tick function every 100ms and then doing the extra computation of dividing the value by 1000, instead of just configuring the the setInterval() to run every 1 second (1000ms)

I was wondering the exact same thing

Steve Gallant
Steve Gallant
14,915 Points

Glad I'm not the only one wondering this! Guil Hernandez ?

1 Answer

Ian Ostrom
seal-mask
.a{fill-rule:evenodd;}techdegree
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,304 Points

The problem is that link doesn't do a good job of explaining the problem. Without understanding it, I can only interpret "calls are throttled to a minimum of once every 4 ms" to mean there is a four second delay. This is the best explanation I could find.

The thing is… browsers don’t really respect that milliseconds thing. You see, historically, web developers have abused setTimeout. A lot. To the point where browsers have had to add mitigations for setTimeout(/* ... */, 0) to avoid locking up the browser’s main thread, because a lot of websites tended to throw around setTimeout(0) like confetti.