timer/counter - new question
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
timer/counter - new question
ok, I know I'm not there yet. Kind of feel dump, but I'll try again.
let's say I have a button for "start"/"stop"
I have a field I'll call "result"
when I click "start" I want to add 1 to the field "result"
1
wait 1 second
2
wait 1 second
3
and so forth.
I want this to go on forever, until I click "stop"
so, I tried to do something like
on mouseUp
repeat with i = 60
wait 1 second
put i into field "result"
end repeat
end mouse up
this works, however, while the script is adding 1 to the field, I can't click on "stop" when the number gets to say 34, or whatever.
how do I create something that I can start and stop at will? and of course I have a follow up. suppose I have three such buttons and corresponding result fields: start/stop 1, start/stop 2, start/stop 3... each with a different frequency, and have them all running at the same time, or have 1 and 3 running and 2 'idle", or 3 running and 1 and 2 "idle"
well, first things first... get one to work.
suggestions apprecitated.
homer
let's say I have a button for "start"/"stop"
I have a field I'll call "result"
when I click "start" I want to add 1 to the field "result"
1
wait 1 second
2
wait 1 second
3
and so forth.
I want this to go on forever, until I click "stop"
so, I tried to do something like
on mouseUp
repeat with i = 60
wait 1 second
put i into field "result"
end repeat
end mouse up
this works, however, while the script is adding 1 to the field, I can't click on "stop" when the number gets to say 34, or whatever.
how do I create something that I can start and stop at will? and of course I have a follow up. suppose I have three such buttons and corresponding result fields: start/stop 1, start/stop 2, start/stop 3... each with a different frequency, and have them all running at the same time, or have 1 and 3 running and 2 'idle", or 3 running and 1 and 2 "idle"
well, first things first... get one to work.
suggestions apprecitated.
homer
Hi homer ~
One approach to the first question:
One approach to the first question:
Code: Select all
local lastNumber=1
on mouseUp
if lastNumber is not empty
then
repeat with i=lastNumber to 60
if the mouseClick is true
then
put fld "result" into lastNumber
exit mouseUp
else
put i into field "result"
wait 1 second
if i = 60
then
put 1 into lastNumber
end if
end if
end repeat
end if
end mouseUp
Last edited by Lynn P. on Sat Aug 04, 2007 1:50 pm, edited 1 time in total.
-
- Posts: 179
- Joined: Sat Apr 08, 2006 11:08 pm
- Location: London, UK
- Contact:
Re: timer/counter - new question
Code: Select all
local sRunning = false
on mouseUp
if not sRunning then
put true into sRunning
incFld
else
put false into sRunning
end if
end mouseUp
on incFld
if sRunning then
add 1 to fld "counter"
send "incFld" to me in 1 second
end if
end incFld
Mark
ok, I'm making progress... and learning
Thanks Mark for the help.
next thing is this.
I created a button with a label "start1"
local sRunning = false
on mouseUp
if not sRunning then
put true into sRunning
incFld
else
put false into sRunning
end if
set the backgroundcolor of me to "0,255,0"
set the label of me to "start1"
end mouseUp
on incFld
if sRunning then
add 1 to fld "counter01"
send "incFld" to me in 1 second
set the backgroundcolor of me to "255,0,0"
set the label of me to "stop1"
end if
end incFld
This all works, however, when the button is clicked and the counter begins, there is a momentary delay before the button changes from label "start1" to "stop1" and from color green to red.
do I have the "set" in the wrong places?
Thanks again for your help.
Homer
Thanks Mark for the help.
next thing is this.
I created a button with a label "start1"
local sRunning = false
on mouseUp
if not sRunning then
put true into sRunning
incFld
else
put false into sRunning
end if
set the backgroundcolor of me to "0,255,0"
set the label of me to "start1"
end mouseUp
on incFld
if sRunning then
add 1 to fld "counter01"
send "incFld" to me in 1 second
set the backgroundcolor of me to "255,0,0"
set the label of me to "stop1"
end if
end incFld
This all works, however, when the button is clicked and the counter begins, there is a momentary delay before the button changes from label "start1" to "stop1" and from color green to red.
do I have the "set" in the wrong places?
Thanks again for your help.
Homer
-
- Posts: 179
- Joined: Sat Apr 08, 2006 11:08 pm
- Location: London, UK
- Contact:
Homer, I'd probably factor the colour changing etc. into a separate handler:
best,
Mark
Code: Select all
local sRunning = false
on mouseUp
if not sRunning then
put true into sRunning
incFld
else
put false into sRunning
end if
updateMe
end mouseUp
on incFld
if sRunning then
add 1 to fld "counter01"
send "incFld" to me in 1 second
end if
end incFld
on updateMe
if sRunning then
set the backgroundcolor of me to "255,0,0"
set the label of me to "stop1"
else
set the backgroundcolor of me to "255,0,0"
set the label of me to "stop1"
end if
end updateMe
Mark
-
- VIP Livecode Opensource Backer
- Posts: 165
- Joined: Mon Jul 16, 2007 11:58 pm
- Location: Las Vegas, NV
Possible Solution I Have Used
Hi Homer,
I'm still fairly new at programming so feel free to take my suggestion with a grain of salt, but I have created similar timers before.
If you want the timer to run until you click on something, why not just do:
repeat until mouseClick
Offhand, I can't remember if you need to put "the" in there as well, which would simply be:
repeat until the mouseClick
With such a script, your repeat will stop instantly the moment you click the mouse after the timer starts, and it doesn't matter what you click on.
Cheers, deeverd
I'm still fairly new at programming so feel free to take my suggestion with a grain of salt, but I have created similar timers before.
If you want the timer to run until you click on something, why not just do:
repeat until mouseClick
Offhand, I can't remember if you need to put "the" in there as well, which would simply be:
repeat until the mouseClick
With such a script, your repeat will stop instantly the moment you click the mouse after the timer starts, and it doesn't matter what you click on.
Cheers, deeverd
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Location: Aalst, Belgium
- Contact:
Hi Homer and Deeverd,
While this 'repeat until the mouseClick' construct works and does stop the timer regardless of where the user clicks, this is an approach that eats up the CPU time as the engine is constantly evaluating the 'mouseClick' function, which will only give a result upon click.
This means that it will ask the operating system hundreds or even thousands of times per second if the user clicked the mouse by any chance.
It is more efficient to wait for the operating system to figure it out and send an event to the engine, which in turn dispatches a message that you handle.
See also this excellent article by Jacque Gay entitled 'Polling the Mouse in MetaCard and Revolution' :
http://www.hyperactivesw.com/polling.html
Hope this helped,
Jan Schenkel.
While this 'repeat until the mouseClick' construct works and does stop the timer regardless of where the user clicks, this is an approach that eats up the CPU time as the engine is constantly evaluating the 'mouseClick' function, which will only give a result upon click.
This means that it will ask the operating system hundreds or even thousands of times per second if the user clicked the mouse by any chance.
It is more efficient to wait for the operating system to figure it out and send an event to the engine, which in turn dispatches a message that you handle.
See also this excellent article by Jacque Gay entitled 'Polling the Mouse in MetaCard and Revolution' :
http://www.hyperactivesw.com/polling.html
Hope this helped,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com