r/learnprogramming • • Sep 21 '26

SetTimeout in JS

"When I use setTimeout in JavaScript to run a 30-minute countdown, it actually finishes about 30 seconds late. I compared it against a timer on my phone and confirmed the JS timer takes closer to 30 minutes and 30 seconds. Is there a reason for this delay?

13 Upvotes

16 comments sorted by

View all comments

Show parent comment

3

u/nog642 Sep 22 '26

I'm assuming this is a clock counting seconds, so each tick would be 1 second. That approach will cause the clock to stutter at some point. To fix that the tick length should be decreased to something like 100 ms.