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 Resetting the Stopwatch

Jose Morales-Mendizabal
Jose Morales-Mendizabal
19,175 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,943 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,332 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.