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 trial

JavaScript

Samuel Durkin
Samuel Durkin
48,402 Points

Triggering JavaScript even when phone goes to sleep

Hello everybody,

This is quite a general question, but I'm currently building a Rails application that relies heavily on timer functionality (the excellent http://keith-wood.name/countdown.html for anybody wondering). It's currently working really nicely, with the exception of if you navigate to a different tab in your web browser or your phone simply goes to sleep.

I'm not really looking for code examples, just wondering whether this something you can reasonably get around. Google's inbuilt timer (where you simply type "timer 20 seconds" into their search box) works wonderfully, but I'm not sure what technology they're using.

Thanks for any help!

Samuel

2 Answers

Hunter Cassidy
Hunter Cassidy
5,674 Points

Hey Samuel,

Here is a stack overflow link that might be useful: http://stackoverflow.com/questions/5927284/how-can-i-make-setinterval-also-work-when-a-tab-is-inactive-in-chrome

It talks about using elapsed time and goes on to mention web workers.

The timer on Google seems to use links to start and stop the timer with custom 'jsaction' html attributes.

Samuel Durkin
Samuel Durkin
48,402 Points

That's an amazingly helpful answer Hunter, in the meantime I'd set up a temporary setTimeout solution which is not nearly as accurate as I'd like for the website.

Am currently travelling for the next week, but looking forward to trying this out when I get back.