button to exit loop...

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

Post Reply
tjo
Posts: 16
Joined: Wed Feb 12, 2014 12:08 am

button to exit loop...

Post by tjo » Fri Apr 11, 2014 5:16 am

I have a small program that displays a new random number every 1200 milliseconds after clicking a button. It uses a loop that repeats while the variable gstop = 0. I have a "stop" button that should set the value of gstop to 1, to exit the loop and stop the program. It looks like this:

Code: Select all

global gstop

on mouseUp
   put 0 into gstop
   repeat while gstop = 0
   put random(99) into field "Label Field"
   wait 1200 milliseconds
   end repeat
end mouseUp
However once the repeat loop is running clicking my "stop" button has no effect. All I can think of is the wait command might be blocking input for most of the time. Is there an easier way to have a button that will simply exit this loop? Can I use the same button to start and stop the loop?

Thanks for any advice or suggestions,

TJ.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: button to exit loop...

Post by Simon » Fri Apr 11, 2014 5:22 am

Hi TJ.
wait is blocking so you have to add:
"with messages"

Code: Select all

 wait 1200 milliseconds with messages
Also check out "send" in the dictionary.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: button to exit loop...

Post by Simon » Fri Apr 11, 2014 5:29 am

more:

Code: Select all

global gstop

on mouseUp
   if the label of me is "Start" then
      set the label of me to "Stop"
      put 0 into gstop
      repeat while gstop = 0
         put random(99) into field "Label Field"
         wait 1200 milliseconds with messages
      end repeat
   else
      set the label of me to "Start"
      put 1 into gstop
   end if
end mouseUp
Way too much fun :)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

tjo
Posts: 16
Joined: Wed Feb 12, 2014 12:08 am

Re: button to exit loop...

Post by tjo » Sun Apr 13, 2014 7:50 pm

Hi Simon,

Thank you for your reply. I think adding the "with messages" was the key. I see now that input was blocked during the "wait" period, so I'm guessing my key presses were not getting through.

Thanks again for all your help,

TJ

Post Reply