timer/counter - new question

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
homer
Posts: 20
Joined: Fri Jun 29, 2007 6:44 pm

timer/counter - new question

Post by homer » Fri Aug 03, 2007 6:20 pm

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

Lynn P.
Posts: 79
Joined: Sun Apr 09, 2006 1:09 pm

Post by Lynn P. » Fri Aug 03, 2007 10:38 pm

Hi homer ~

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.

homer
Posts: 20
Joined: Fri Jun 29, 2007 6:44 pm

Post by homer » Sat Aug 04, 2007 4:46 am

thanks, but that didn't work.

I copied the script into a button. I created a field named "result"

when i clicked on the button, nothing happened.

more assistance would be appreciated.

thanks

homer

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Location: London, UK
Contact:

Re: timer/counter - new question

Post by Mark Smith » Sat Aug 04, 2007 9:29 am

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
Best,

Mark

Lynn P.
Posts: 79
Joined: Sun Apr 09, 2006 1:09 pm

Post by Lynn P. » Sat Aug 04, 2007 1:55 pm

homer wrote:thanks, but that didn't work.
...
````````````````
Oops. :oops:
Forgot to initialize the local variable. It's fixed now and should work.
My solution only counts to 60 as you indicated in your own code, but Mark's suggestion will go on forever as you also stated you wanted it to do.

homer
Posts: 20
Joined: Fri Jun 29, 2007 6:44 pm

Post by homer » Sat Aug 04, 2007 7:15 pm

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

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Location: London, UK
Contact:

Post by Mark Smith » Sun Aug 05, 2007 7:00 pm

Homer, I'd probably factor the colour changing etc. into a separate handler:

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
best,

Mark

homer
Posts: 20
Joined: Fri Jun 29, 2007 6:44 pm

Post by homer » Mon Aug 06, 2007 6:46 pm

Mark,

Thanks for the suggestion. I will try that tonite. I had actually thought along those line -- a separate handler to "toggle" the state, but I wasn't quite sure where in the "mouseUp" it should go.

I'll let you know how it works out.

Thanks.

Homer

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm
Location: Las Vegas, NV

Possible Solution I Have Used

Post by deeverd » Mon Aug 06, 2007 9:02 pm

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

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Mon Aug 06, 2007 9:38 pm

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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”