Is there a way to detect when a iPhone goes to sleep and when the mobile safari browser has been minimized?

Reason why I ask is that I made a javascript countdown timer for a project that I'm working on and when you minimized or when the iPhone goes to sleep the Javascript stops in the browser. Meaning when the user comes back to the page the countdown timer is way off. What are some ways that I can fix this?