What edge cases should you handle in a stopwatch?
Use Date.now() for accuracy (not interval counting), preserve elapsed on pause/resume, format with leading zeros, lap list with lap numbers, and clear the interval on reset to avoid memory leaks.
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.
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.
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.
Still have questions?
Browse all our FAQs or reach out to our support team
