Accurate Timer
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Accurate Timer
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
Thanks,
Jarren
Re: Accurate Timer
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
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
Aha, makes perfect sense, thanks a bunch!
Re: Accurate Timer
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
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
-
- Posts: 5
- Joined: Wed May 04, 2011 12:19 am
Re: Accurate Timer
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
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:
I bet this can be improved with clearer thinking.
Craig
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
Craig
Re: Accurate Timer
..........
Last edited by [-hh] on Wed Aug 13, 2014 11:43 am, edited 1 time in total.
shiftLock happens
Re: Accurate Timer
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!
-
- Posts: 92
- Joined: Thu Feb 14, 2013 7:17 pm
Re: Accurate Timer
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
Topic really interesting. And if you want to add even tenths of a second?
Re: Accurate Timer
Tenths?
Just modify the code a little. This in a button script,and you need that field "f6":
Craig Newman
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
Re: Accurate Timer
Oops, maybe Tenths is not the current word...
Thank you dunbarx!

Thank you dunbarx!
Re: Accurate Timer
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
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
..........
Last edited by [-hh] on Wed Aug 13, 2014 11:43 am, edited 2 times in total.
shiftLock happens
Re: Accurate Timer
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
Craig