Page 1 of 1

question about a timer

Posted: Thu Jan 19, 2012 5:48 pm
by KennyR
Hello everyone....I was hoping I could get some help on a timer I have modified. I found some info about creating a timer and I added a few things to make it useful for my program, but I have a question about the counting process. I will attach the code and explain...

Code: Select all

on mouseUp

      if the label of me = "start" then 
      set the label of me to "stop"
      set the startingTime of me to the seconds
      set the oldTime of me to line 1 of fld "timer"

      send "countUp" to me in 1
   else
      set the label of me to "start"
   end if
end mouseUp

on countUp
   
   put (the seconds - the startingTime of me) + the oldTime of me into fld "timer"
   if the label of btn "start" = "stop" then send "countUp" to me in 1

if fld "timer" >=60 then
   put "00" into fld "timer"
   add "1" to fld "minute"
   set the startingTime of me to the seconds
      set the oldTime of me to line 1 of fld "timer"
      send "countUp" to me in 1
else
   end if
end countUp

When the seconds tick up to 60, the counter resets and goes back to zero....but instead of looking like "00" , it looks like "0" then proceeds to count up by 1. I would like the numbers to be able to count up in increments of (01,02,03,etc), so that it looks more like a clock. Any suggestions?

Re: question about a timer

Posted: Thu Jan 19, 2012 7:46 pm
by dunbarx
Why not just prepend a "0" if the number of chars of your data = 1?

Craig Newman

Re: question about a timer

Posted: Thu Jan 19, 2012 8:15 pm
by mwieder
That certainly seems like the easiest way to deal with it. The "add" calculation is "helpfully" removing the leading zeros for you, so you'll need to put them in as needed.

Re: question about a timer

Posted: Thu Jan 19, 2012 10:05 pm
by KennyR
I appreciate the response but i am a noob with no idea what or how to do this....My coding experience is lousy and I am trying to learn....I have tried altering the code with a leading "0" in every line of code that is applicable , but nothing changes...I have tried to set the label's text to "00" but that doesn't work...can you give me an example? Sorry again...and thanks

Re: question about a timer

Posted: Thu Jan 19, 2012 10:09 pm
by mwieder
NP. Try adding the following lines after your "add" line:

Code: Select all

   if fld "minute" < 10 then
      put "0" before fld "minute"
   end if

Re: question about a timer

Posted: Thu Jan 19, 2012 11:11 pm
by KennyR
mwieder wrote:NP. Try adding the following lines after your "add" line:

Code: Select all

   if fld "minute" < 10 then
      put "0" before fld "minute"
   end if
Thank you very very much! I now have the timer working as I wanted....I have literally been working on this ALL DAY! Ha! I need to get used to the language of LIveCode and start trying plain english commands from now on....I have been learning the C++ language and it would not be as simple as saying "put a "0" before fld "minute".....Thanks again for helping out!

Re: question about a timer

Posted: Thu Jan 19, 2012 11:19 pm
by mwieder
I think text processing is one of Livecode's strongest suits. Try doing something like

Code: Select all

put "<font color=" & quote & "red" & quote & ">" & tWarning & "</font>" before word 3 of line 7 of field "xyzzy"
in C++. It's a real pain.

Re: question about a timer

Posted: Thu Jan 19, 2012 11:41 pm
by sturgis
You can also use the numberformat to force 2 digits.

If you add the following line to the top of your countup handler you should get the behavior you want.

set the numberformat to "00"

Re: question about a timer

Posted: Fri Jan 20, 2012 12:26 am
by sturgis
Just noticed something in the script.

If the label of the button is stop, you do a send in time, but you also do a send in time if the seconds >= 60

Over time you will end up getting processor bound because every 60 seconds you will add yet another message to the queue. So first 60 seconds it will "send to me in 1.." at 60, you get 2 in queue, 120 3 in queue until you hit overload level. Don't need the 1 in the 60 second test because you have already taken care of it up above.

Re: question about a timer

Posted: Fri Jan 20, 2012 12:28 am
by dunbarx
Sturgis.

Of course. I am so used to only fooling around with the right side of the decimal point, I forgot that this property is just a flexible to the left.

So much better...

Craig Newman