Page 1 of 1

Countdown

Posted: Mon Aug 03, 2009 3:42 am
by alfredomora
Hi :-)

I want to make a countdown from 60 to 0, and I want to make it visual with a progress bar. Can you help me?

Greetings from Mexico,

Alfredo Mora Izaguirre

Posted: Mon Aug 03, 2009 5:59 am
by Janschenkel
Create a new stack, drag a progress bar and a button onto it. Select the pogress bar, and use the Inspector palette to set its name to "MyProgressBar", its startValue to 0 and its endValue to 60. Then set the name of the button to "Countdown" and set its script to:

Code: Select all

on mouseUp
  repeat with i = 0 to 59
    set the thumbPosition of scrollbar "MyProgressBar" to i
    put (60 - i) && "seconds left"
    wait 1 second
  end repeat
  set the thumbPosition of scrollbar "MyProgressBar" to 60
  answer "Your minute is up!"
end mouseUp
If the user needs to interact with the stack while the countdown is happening, then replace the line

Code: Select all

wait 1 second
with

Code: Select all

wait 1 second with messages
This way, other buttons can receve and handle events as the clock keeps ticking. So drag another button onto the stack and set its script to

Code: Select all

on mouseUp
  send "CancelCountdown" to button "Countdown"
end mouseUp
Next modify the script of button "Countdown" slightly to

Code: Select all

on mouseUp
  set the uStatus of me to "started"
  repeat with i = 0 to 59
    set the thumbPosition of scrollbar "MyProgressBar" to i
    put (60 - i) && "seconds left"
    wait 1 second with messages
    if the uStatus of me is "cancelled" then
      put "Cancel was clicked!"
      exit mouseUp
    end if
  end repeat
  set the thumbPosition of scrollbar "MyProgressBar" to 60
  put "Your minute is up!"
end mouseUp
on CancelCountdown
  set the uStatus of me to "cancelled"
end CancelCountdown
Now the clock is ticking, and you know how to stop it when the user clicks a button. That should get you on your way, I think ;-)

Jan Schenkel.

Thank you!

Posted: Mon Aug 03, 2009 8:57 pm
by alfredomora
Dear Jan,

Thank you very much, all works fine!

Greetings from Mexico,

Alfredo Mora Izaguirre

Re: Countdown

Posted: Sat Jun 08, 2013 5:53 pm
by Mag
Hi all,

I'm implementing this code, it works fine but it refreshes the message box only when I comment the line which changes the label of a button, the same if I put a line that changes the custom properties of the target button or if I want to update a progress bar.

Code: Select all

        repeat with i = 0 to 10
             put (10 - i) && "seconds left"
             -- set the label of button "timerButton" to (10 - i)
             wait 1 second with messages
        end repeat
Any advice?

Re: Countdown

Posted: Sat Jun 08, 2013 6:27 pm
by jmburnod
Hi mag,
it refreshes the message box only when I comment the line which changes the label of a button
I test your script (LC 5.5.4) and it works. I can see the label change and i & "seconds left" in the messagebox

Best regards
Jean-Marc

Re: Countdown

Posted: Sat Jun 08, 2013 8:56 pm
by Simon
Hi Mag,
Works here in LC 6.0.0

Simon

Re: Countdown

Posted: Sat Jun 08, 2013 11:28 pm
by Mag
Oops, I apologize. I followed the advice of the grandmother (restart LiveCode) and everything started to work properly. Maybe I intoxicated the memory with my previous experiments. Thank you so much Simon and jmburnod, your posts convinced me to try again.