Multiple Timers

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
ukaussie
Posts: 23
Joined: Mon Sep 28, 2009 9:04 pm

Multiple Timers

Post by ukaussie » Thu Apr 15, 2010 5:44 pm

Hi,

I'm writing a little app that requires multiple timers on the same card. This is the code i have in a 'Start' button:

Code: Select all

local tMin1, tSec1

on mouseUp
   if the label of me is "Start" then
      set the label of me to "Stop"
      put 0 into tMin1
      put 0 into tSec1
      repeat while the label of me is "Stop"
         wait 1 second with messages
         add 1 to tSec1
         if tSec1 < 60 then
            if tSec1 <= 9 then
               put "0" & tSec1 into field "1Sec"
            else
               put tSec1 into field "1Sec"
            end if
         else
            if tSec1 = 60 then
               put "00" into field "1Sec"
            end if
            put 0 into tSec1
            add 1 to tMin1
            if tMin <= 9 then
               put "0" & tMin1 into field "1Min"
            else
               put tMin1 into field "1Min"
            end if
         end if
      end repeat
   else
      set the label of me to "Start"
   end if
end mouseUp
I've simply copied the whole setup (start button, fields to display the time etc) another two times and placed them below the first one (i've modified the local variables though to tMin2, tSec2; tMin3, tSec3).

My problem is that when i start the second timer the first timer freezes. As you can see in the code i have used 'with messages' on the 'wait' command, so i'm confused as to why the freezing is happening.

Grant.

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Multiple Timers

Post by Regulae » Thu Apr 15, 2010 6:56 pm

Hi Grant,

A solution which worked for me was, in each timer button, the script:

Code: Select all

local tMin1, tSec1

on mouseUp
   if the label of me is "Start" then
      set the label of me to "Stop"
      put 0 into tMin1
      put 0 into tSec1
      send "Timer1" to me in 1 second
   else
      set the label of me to "Start"
   end if
end mouseUp

on Timer1
   add 1 to tSec1
   if tSec1 < 60 then
      if tSec1 <= 9 then
         put "0" & tSec1 into field "1Sec"
      else
         put tSec1 into field "1Sec"
      end if
   else
      if tSec1 = 60 then
         put "00" into field "1Sec"
      end if
      put 0 into tSec1
      add 1 to tMin1
      if tMin <= 9 then
         put "0" & tMin1 into field "1Min"
      else
         put tMin1 into field "1Min"
      end if
   end if
   if the label of me <> "Start" then
      send "Timer1" to me in 1 second
   end if
end Timer1
... with the second timer having a handler named Timer2, the third Timer3, and so forth. I think what is happening in your original version is a virtual “nested” repeat. During the 1 second wait with messages when one timer is running, the mouseUp to the second timer is received. The second timer’s repeat takes over, but during its “wait with messages”, no message is being sent by the first timer- it’s still “mid-repeat”. We get around this by sending the messages Timer1, Timer2, Timer3. Other folks may have better ideas, however.

Yours,

Michael

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Multiple Timers

Post by mwieder » Thu Apr 15, 2010 7:29 pm

'Send in time' is definitely the way to go for this. Otherwise you're stuck inside the mouseUp handler and everything will be synchronous. The way out of that is to make the timers separate handlers that trigger themselves after the initial push, as you've done there.

ukaussie
Posts: 23
Joined: Mon Sep 28, 2009 9:04 pm

Re: Multiple Timers

Post by ukaussie » Thu Apr 15, 2010 9:16 pm

Hi Michael,

That did the trick! Thanks for your help.

Grant.

Post Reply