[Closed] Countdown timer to fn?
hi,
I’ve been playing around with the ui timer, but i have a few questions on what it can actually do. Here is the script from the documenation:
rollout test "Test Timer"
(
timer clock "testClock" interval:1000 --tick once a second
label test"1"
on clock tick do
(
valUp = (test.text as integer)+1
test.text = valUp as string
)
)
createDialog test
The first question: is there a way to start at a specific time on the timer, like 100?
I just used a -1 in the on…do block instead of +1. but it goes in negative direction. which would be ok for my next question:
Is there a way to reach a certain time like 10 and then have that call a function? I’m guessing the answer to both of these is no, b/c its just a string that’s being displayed, but “tick” does seem to be a inherent property so i was wondering if you could set up a loop or array of ticks then call a function after a set of ticks
I hope that this will helps you.
rollout test "Test Timer"
(
local tickCnt = 0
timer clock "testClock" interval:10 active:false--tick once a second
label test "1"
button btn_startClock "Start clock"
button btn_stopClocl "Stop clock"
on clock tick do
(
tickCnt += 1
case tickCnt of
(
10: (print "Tick 10 times")
100: (print "Tick 100 times")
)
valUp = (test.text as integer)+1
test.text = valUp as string
)
on btn_startClock pressed do
(
clock.active = true
)
on btn_stopClocl pressed do
(
clock.active = false
-- reset the tick count variable if you want.
tickCnt = 0
)
)
createDialog test
About your first question – using the tickCnt variable you can choose when to start your code/function or whatever you want. Start the timer and when the tickCnt is 10, or any other value start your code. No need to try to start the counter from value 100.
The second question – again using the tickCnt you can choose when to start custom function. No need of for loop.
You are right.
rollout test "Test Timer"
(
timer clock "testClock" interval:10 active:false--tick once a second
label test "1"
button btn_startClock "Start clock"
button btn_stopClocl "Stop clock"
button btn_printCurrentTick "Print Current Tick"
on clock tick do
(
case clock.ticks of
(
10: (print "Tick 10 times")
100: (print "Tick 100 times")
)
valUp = (test.text as integer)+1
test.text = valUp as string
)
on btn_startClock pressed do
(
clock.active = true
)
on btn_stopClocl pressed do
(
clock.active = false
-- reset the tick count variable if you want.
tickCnt = 0
)
on btn_printCurrentTick pressed do
(
print clock.ticks
)
)
createDialog test
a quick question;
i’m reading up on case expressions, i’m wondering if you could use an “if expression” instead of a case expression in this context?
The documentation is a little confusing b/c it defines the case expression:
The case expression is used to select an expression to be evaluated from a set of labeled expressions based on a test value compared against the labels.
I was looking at the code you gave, and was surprised you could use “Label “1″” to set the time. I thought labels were static. Just wondering why you used the case expression and when you would normally choose to use one.
You can change the text(caption) of labels, buttons, checkboxes controls. They are not static.
About case vs. If then else I hope this will helps you:
-- 10:, 100: can be nay value
-- Using case of expression you have nice and clean code.
-- the evaluation of the case expression will stops when the match is found.
case clock.ticks of
(
10: (print "Tick 10 times")
100: (print "Tick 100 times")
default: (print "----------")
)
-- this will do the same as case of expression.
-- The "problem" here is if you have to check clock.ticks for 100 possible values you will have
-- too "deep" nested [i]If then else[/i].
if clock.ticks == 10 then
print "Tick 10 times"
else
(
if clock.ticks == 100 then
print "Tick 100 times"
else
(
print "----------"
)
)
-- this also will do the same as the codes above.
-- The problem here is that MAX sill evaluate all lines. If the [i]clocl.Ticks == 10[/i] in the listener will be printed "Tick 10 times",
-- but the next lines also will be evaluated, which is not needed.
if clock.ticks == 10 do print "Tick 10 times"
if clock.ticks == 100 do print "Tick 100 times"
if clock.ticks != 10 and clock.ticks != 100 do print "----------"
check this snippet:
try(destroydialog timerTest) catch()
rollout timerTest "Timer Test" width:200
(
timer tm interval:100
label ticks "" align:#center
on tm tick do ticks.text = tm.ticks as string
on timerTest open do
(
tm.active
while not keyboard.escpressed do ()
)
)
createdialog timerTest
as you see the rollout timer doesn’t work when the system is busy. so it’s not a good solution to check count of ticks to measure time interval. of course the 10th or 100th tick will happen some time, but nothing can guaranty that it will be at 10*interval moment.