Page 1 of 2
Accurate Timer
Posted: Wed Aug 14, 2013 2:42 am
by Jarren
I currently have a timer in my app, which counts down from 2 minutes 30 seconds. It simply calls the UpdateTimer handler every 1 second by using "Send UpdateTimer to me in 1 second". This works fine and dandy, but the accuracy is pretty bad. For example, I ran the timer on my iPad 4th Generation, and my iPhone 4. By the time the iPad timer was done, the iPhone timer had 4 seconds to go. Is there a more accurate way to implement a more accurate timer, or am I stuck?
Thanks,
Jarren
Re: Accurate Timer
Posted: Wed Aug 14, 2013 2:46 am
by dunbarx
Your idea of "sending in time" is correct.
Try a different approach, to send a message to a handler that returns the current seconds. In this way you are always using the system clock, instead of a local script-based timer, which might be encumbered in who knows what ways.
Get out, in other words, of measuring, and ask a higher authority.
Craig Newman
Re: Accurate Timer
Posted: Wed Aug 14, 2013 2:54 am
by Jarren
Aha, makes perfect sense, thanks a bunch!
Re: Accurate Timer
Posted: Wed Aug 14, 2013 3:49 am
by dunbarx
Hi.
I was thinking about this. One advantage of your old method was that the display counted down in an orderly way, albeit not accurately. In other words, it counted down without missing a digit, even though those digits wandered off the seconds they were supposed to represent.
In the new method, the seconds will be accurate, but that wandering thing might eventually have the display skip a digit. This because the calling handler is not linked to the actual time, as discussed above.
So, a homework project. Can you think of a way to use the system time itself to call the handler? In this way the seconds will "load themselves".
Craig
Re: Accurate Timer
Posted: Wed Aug 14, 2013 9:13 pm
by awesomeandrew
Okay Craig, I give up--how do you do it? I'm a real newbie, so this really is beyond me, but timers are important to me, so I'm dying to know! Thanks, Andrew
Re: Accurate Timer
Posted: Wed Aug 14, 2013 10:15 pm
by dunbarx
Many ways to do this. It just is a matter of using the system time to do the counting. Most of the time here the display is reloaded with the same second indicator. We only perceive it changed when, well, it changes. Another tack could be to send the recursive handler call only when the difference in the count has indeed changed. Not sure if this releases overhead back to the handler that actually does some useful work. With a button and a field, in the button script:
Code: Select all
on mouseUp
put the seconds into startingTime
put "" into fld 1
updateTimer startingTime
end mouseUp
on updateTimer startingTime
get the seconds - startingTime
if it > 4 then --put your 150 here when you have seen it work a few times
put "Done"
exit updateTimer
end if
put the seconds - startingTime into line 1 of fld 1
doOtherStuff
wait 0 with messages
updateTimer startingTime
end updateTimer
on doOtherStuff
put "Current useless output is:" && random(999) && any char of "ABCDEF" into line 2 of fld 1
end doOtherStuff
I bet this can be improved with clearer thinking.
Craig
Re: Accurate Timer
Posted: Sat Aug 17, 2013 11:18 pm
by [-hh]
..........
Re: Accurate Timer
Posted: Wed Aug 21, 2013 2:20 pm
by Jarren
Thanks, hhBUSwU7p! That exactly what I was looking for! The timer works perfectly now, and in the long run ends up using 50% less code!
Re: Accurate Timer
Posted: Tue Nov 26, 2013 2:37 am
by jekyllandhyde
Thanks hh for the timer code. Very nice. Quick question. I have a series of fields with seconds values that I want to count down. I would like to have field1 count down to 0 then field 2 starts immediately and count down to 0, etc. So in hh's code "Countdowndisplay" field will change and x will be updated with a new value in seconds to countdown. I spent four hours on this and I'm stumped. Right now my button script calls myCountDown and the passes x's value to the "on MyCountdown" code in the card script.
Re: Accurate Timer
Posted: Tue Nov 26, 2013 10:43 am
by Mag
Topic really interesting. And if you want to add even tenths of a second?
Re: Accurate Timer
Posted: Tue Nov 26, 2013 3:05 pm
by dunbarx
Tenths?
Just modify the code a little. This in a button script,and you need that field "f6":
Code: Select all
on mouseUp
put the milliseconds into startingTime
put "" into fld "f6"
updateTimer startingTime
end mouseUp
on updateTimer startingTime
get the milliseconds - startingTime
if it > 4000 then -- "4000" just runs for 4 seconds
put "Done"
exit updateTimer
end if
get (the milliseconds - startingTime)
if it < 1000 then
put "0" & "." & char 1 of it into line 1 of fld "f6"
else
put char 1 of it & "." & char 2 of it into line 1 of fld "f6"
end if
doOtherStuff
wait 0 with messages
updateTimer startingTime
end updateTimer
on doOtherStuff
put "Current useless output is:" && random(999) && any char of "ABCDEF" into line 2 of fld "f6"
end doOtherStuff
Craig Newman
Re: Accurate Timer
Posted: Tue Nov 26, 2013 3:18 pm
by Mag
Oops, maybe Tenths is not the current word...
Thank you dunbarx!
Re: Accurate Timer
Posted: Tue Nov 26, 2013 3:25 pm
by dunbarx
Mag.
I had an "oops" edit in there, saying I had a glitch in the code. But then went back and fixed it, and removed the edit. It works pretty well now...
Craig
Re: Accurate Timer
Posted: Tue Nov 26, 2013 6:24 pm
by [-hh]
..........
Re: Accurate Timer
Posted: Tue Nov 26, 2013 6:41 pm
by dunbarx
Trying to figure a way to slip a jape in there. Still worried that we have only one "h", and the negative one at that, and I recall a couple more are required to get the second one. Seems like plenty of fodder here, with those split personalities.
Craig