Timer Confusion

351 views
Skip to first unread message

Ben Pekala

unread,
Oct 24, 2014, 2:53:20 AM10/24/14
to mitappinv...@googlegroups.com

How do the timers on App Inventor work? Me and my partner designed an app that we planned on using for the debate team that included a simple timer that would count down. After solving all of out issues with the app and after developing it we noticed that the timer was off by a few seconds. When we did trials on different phones using different methods of connection(eventually only using downloads, not the connection which is slow and doesn't work with the timer) we discovered that its off by different amounts each time, and by different amounts on different devices. THIS IS THE SAME EXACT APP. Can someone explain to me how we might go about solving this issue? We've tampered with the time increment on the timer several times to try to adjust for it but it still remains inconsistent and inaccurate. Maybe a deeper understanding of how the AppInventor timer works would help ? Thanks - Ben Pekala 

Markus Olszewski

unread,
Oct 24, 2014, 3:21:55 AM10/24/14
to mitappinv...@googlegroups.com
Hi Ben,

It could be that you forgot to uncheck the "TimerAlwaysFires" component.

If you're making an app that is counting down a second at a time then make a variable and set it to 0 (unless it will be the same each time), adjust the variable with a TextBox.text (or other method). Set the clock interval to 1000 and uncheck both tick boxes. In blocks section set a Button1.Clicked to [set] timer1.TimerEnabled to true. Next set the timer1.Timer to set the viariable to variable - 1 (depends what you call it). and then finally set the variable to a label.

Hope it helps.

Markus

Abraham Getzler

unread,
Oct 24, 2014, 10:52:42 AM10/24/14
to mitappinv...@googlegroups.com
Here's a self-correcting approach -

Don't try to count clock ticks.
That leads to cumulative error.

Instead, capture the instant you start the clock into a variable,
run the clock faster than 2 ticks per second, and each tick
calculate the difference between Now() and your starting instant,
first in milliseconds then converted to displayable minutes and seconds.

Because Now() (and the system time alternative) are always accurate,
you will never be off by more than a second.

ABG


SteveJG

unread,
Oct 24, 2014, 11:08:16 AM10/24/14
to mitappinv...@googlegroups.com
ABG's solution is especially good.   AI2's clock is relatively accurate but as you discovered, not precise, especially for small intervals of time.      App Inventor 2 is an Event Driven compiler.   It means that when you elicit a process, that process continues until it concludes.  You can not simultaneously have several process working (multi-threading is not supported in AI2).    I suspect, but do not know for sure, that if your debate app has other processes running frequently, that could affect the clock timing ...perhaps ABG/s solution will ameliorate that.   The point here is that if you require precise time measurements and simultaneously require other Android activities, you will need to test the algorithm you develop stand alone (without other processes working) and in conjunction with any other activities you place on your app to be certain that the more accurate timing will suit your purposes.

Also be aware, your timer is useless if your phone goes to sleep ... AI2 apps do not run in the background.    Search the forum, there are lots of workarounds for keeping the phone active for long periods of time.   If you do so, realize the devices battery will rapidly deplete.  This may or may not be an issue you have to consider.

Good luck with your project.  When you finish the timer part of your app using ABG's suggestion, please do consider posting some blocks so others too may benefit.  Thanks.

Regards,
Steve
Reply all
Reply to author
Forward
0 new messages