Count-down Timer Function

Dar Scott dsc at swcp.com
Wed Mar 13 15:08:00 EST 2002


On Wednesday, March 13, 2002, at 09:36 AM, Kim L. Smith wrote:
> I also need a countdown timer function if anyone has any ideas. I need
> to use it for both Mac and PC so it can't be an XCMD, I think.

I'm a Revolution newbie, but I'm willing to comment.  Take my ideas 
with a grain of salt.


Do you mean you want something to happen in some time period?  Then 
use send like this:

send timesUp to me in 60 seconds


Do you mean you want to call a function that returns the time until 
some some "timer" runs out?

In the handler to set the timer for some time (in ms), get 
milliseconds(), add the time to that and save it in a variable.  In 
the function subtract milliseconds() from the value in the variable 
and return that.


Do you mean you want to display the time until something happens?

Start with a field and add handlers to make it work as a timer.  
Display the remaining time on a regular basis.  Calculate the 
remaining time and display text based on the above function.  Set 
the "timer" for that function when the timer is started.

Now for displaying on a regular basis...  I read somewhere that 
using "send" is better than "idle".  Make a handler that calls the 
display handler and takes care of those things that occur when the 
count gets to or past zero.  If the count is not to or past zero, 
then it should also send a message to itself in some short time 
period (a fraction of the display resolution).  The timer start 
handler should also call this after the timer is set up.

You probably want to keep from calling the regular handler when the 
timer is restarted; keep a flag variable that indicates that the 
timer is on.

You might want to handle what happens when the user goes to another 
card (or tries to).  I don't have any good ideas here.


Alternatives:
If you don't mind accumulated errors, you can set the time in the 
send to the resolution you want and count down a timer.  This puts 
the least load on the system.  If you can't have accumulated 
errors, use the above method and either use a time in send that is 
several times smaller than the resolution you need or adjust it to 
lock in on just after a digit change.  The first of these last two 
might make the display look a little ragged; you might need to make 
a judgment call.

You might want to have something occur when the count gets to 
zero.  The easiest way is to do a send just before calling start on 
the timer.  However, if the timer can be paused or restarted, then 
it might be better to have the regular handler take care of what 
happens at zero.


Yikes!  It looks like I got to babbling.

Dar Scott




More information about the use-livecode mailing list