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 trialJose Morales-Mendizabal
19,175 PointsWhy 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)
Steve Gallant
14,943 PointsGlad I'm not the only one wondering this! Guil Hernandez ?
1 Answer
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,529 PointsBecause it is not always accurate.. https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Reasons_for_delays_longer_than_specified
This is why Guil is tracking current time and previous time to make sure that timing is accurate
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,332 PointsThe 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.
Teresa Plaz
12,758 PointsTeresa Plaz
12,758 PointsI was wondering the exact same thing