stopping a send/repeat loop?

Dar Scott dsc at swcp.com
Sun Jul 20 19:46:03 EDT 2003


On Sunday, July 20, 2003, at 05:36 PM, John Tenny wrote:

> Only it doesn't. I'm sure it simpler than this. Sometimes I can get 
> one clock working, but the second button won't stop it.

Here's more while you are pondering the earlier mail.

While you are getting the hang of this, work with just one timer.

There are a few issues here.  One is starting and stopping the send 
cycle (I call it a simple message machine).  The other is dealing with 
drift because of delays in executing the message.  A third is where 
setting numberFormat goes.

Three approaches to starting and stopping cycles:
1.  Use a state variable that has running, stopped and winding-down 
state
2.  Use a state variable with empty for stopped and the message ID for 
running
3.  Stop by name using pendingMessages() even before starting

I don't think #1 will work well for you because some folks might click 
fast.  Let's go with # 2.

Here is a pattern that should work for you.

local timer1ID
constant timer1Period = "1.0"

on startTimer
   stopTimer  -- alternately exit if timer1ID is not empty
   send "countDown" to me in 0 secs -- or timer1Period secs depending...
   put the result into timer1ID
end startTimer

on stopTimer
   cancel timer1ID
   put empty into timer1Id
end stopTimer

on countDown
   doTimer1Stuff  -- set the numberFormat in here
   send countDown to me in timer1Period
   put the result into timer1ID
end countDown

Put this in the card script for now.  Set up buttons that connect call 
startTimer and stopTimer respectively.

Play with that.  Watch pending messages with the message box.

To expand to two timers, add scripts in the card for timer 2, but both 
stops in the stop button, but start of one and stop of the other in the 
two start buttons.  Move scripts later to, say, field or timer group.

Concerning drift...  It won't be bad for the tests, but when things get 
active or long, try remembering the seconds at the start and then 
subtracting that from the seconds at the display time.

I didn't look at the code in the bulk of your timer counting and 
display.

I hope this points you in the right direction.

Dar Scott







More information about the use-livecode mailing list