Facebook Pixel

Why should you use Date.now() instead of counting intervals in a stopwatch?

Because setInterval is not exact. The interval may fire late (due to event loop, throttling). Counting intervals accumulates drift. Using Date.now() calculates the actual elapsed time, which is always accurate.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in How to Build a Stopwatch in an Interview

Use Date.now() for accurate timing (not interval counting). Track startTime and elapsed. Start sets startTime and runs setInterval. Stop adds the difference to elapsed. Lap records the current time. Reset clears everything. Format with leading zeros.

On stop, add (Date.now() - startTime) to elapsed. On start (resume), set startTime = Date.now() again. The total is always elapsed + (Date.now() - startTime). This preserves the elapsed time across pause/resume.

Convert milliseconds to minutes, seconds, and milliseconds. Use padStart for leading zeros: String(minutes).padStart(2, '0'). Format as MM:SS.mmm or HH:MM:SS.mmm.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.