Accurate Timer

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Jarren
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 21
Joined: Sat Jan 19, 2013 3:31 am

Accurate Timer

Post by Jarren » Wed Aug 14, 2013 2:42 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Accurate Timer

Post by dunbarx » Wed Aug 14, 2013 2:46 am

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

Jarren
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 21
Joined: Sat Jan 19, 2013 3:31 am

Re: Accurate Timer

Post by Jarren » Wed Aug 14, 2013 2:54 am

Aha, makes perfect sense, thanks a bunch!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Accurate Timer

Post by dunbarx » Wed Aug 14, 2013 3:49 am

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

awesomeandrew
Posts: 5
Joined: Wed May 04, 2011 12:19 am

Re: Accurate Timer

Post by awesomeandrew » Wed Aug 14, 2013 9:13 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Accurate Timer

Post by dunbarx » Wed Aug 14, 2013 10:15 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Accurate Timer

Post by [-hh] » Sat Aug 17, 2013 11:18 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:43 am, edited 1 time in total.
shiftLock happens

Jarren
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 21
Joined: Sat Jan 19, 2013 3:31 am

Re: Accurate Timer

Post by Jarren » Wed Aug 21, 2013 2:20 pm

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!

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Accurate Timer

Post by jekyllandhyde » Tue Nov 26, 2013 2:37 am

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.

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Accurate Timer

Post by Mag » Tue Nov 26, 2013 10:43 am

Topic really interesting. And if you want to add even tenths of a second?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Accurate Timer

Post by dunbarx » Tue Nov 26, 2013 3:05 pm

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

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Accurate Timer

Post by Mag » Tue Nov 26, 2013 3:18 pm

Oops, maybe Tenths is not the current word... :oops:

Thank you dunbarx!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Accurate Timer

Post by dunbarx » Tue Nov 26, 2013 3:25 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Accurate Timer

Post by [-hh] » Tue Nov 26, 2013 6:24 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:43 am, edited 2 times in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Accurate Timer

Post by dunbarx » Tue Nov 26, 2013 6:41 pm

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

Post Reply