How to keep $interval running when the screen is off

I have an $interval function that i use for show a timer (mm:ss) and every X seconds play a sound. When the screen goes off and the smartphone is not attached on the computer via USB (this means that i can’t use genymotion or even adb logcat to track the problem) the timer/$interval starts delaying and for example it takes 1:50 real minutes for 1 minute in the app. Any advice?

Thank you

You should always go off of the system time instead of relying that a 1 second timer will always fire exactly every 1 second (if that’s what you’re doing.) You can even have it as a timeout instead of interval, and attempt to run faster, say every 100MS, and call itself on the next run.

Then, compare against the system time to figure out how much time has elapsed (save the time value when the timer started) and that should be much more accurate.

You can have it use local storage to keep track of if it’s already played a sound or not if the timeout runs multiple times in the same second.

Not so much an Ionic question really, but hope it helps you out some!

1 Like

Thank you! I resolved following your tip and using angular-timer directive with the end-time attribute