Simple count up timer

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Simple count up timer

Post by croivo » Mon Apr 13, 2015 6:16 pm

I have this simple count up script... The bug happens when you multiple times click on the start/stop buttons in very short time... Than the clock goes crazy...

I need to somehow block the count up script if the button is pressed more than 1 time in 1 second. Any ideas?

Code: Select all

on mouseUp
   put "Start Clock" into startLabel
   put "Stop Clock" into stopLabel
   
   if the label of me is startLabel then
      set the label of me to stopLabel
      set the backgroundcolor of me to green
      DoCountUp
      
   else
      set the label of me to startLabel
      set the backgroundcolor of me to red
   end if
end mouseUp

on DoCountUp
   put "Start Clock" into startLabel
   put "Stop Clock" into stopLabel
   
   if the label of ME is stopLabel then
      set the numberformat to 00
      add 1 to field GameClockSec
      if field GameClockSec = 60 then
         add 1 to field GameClockMin
         put 00 into field GameClockSec
      else
      end if
      send "DoCountUp tNewTime" to me in 1 second
   end if
end DoCountUp

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Simple count up timer

Post by dunbarx » Mon Apr 13, 2015 7:50 pm

Try this:

Code: Select all

local holdTime
on mouseUp
      put "Start Clock" into startLabel
      put "Stop Clock" into stopLabel
      
      if the label of me is startLabel and the ticks - holdTime >30 then  --NEW
            set the label of me to stopLabel
            set the backgroundcolor of me to green
      put the ticks into holdTime -- NEW
            DoCountUp
      else
            set the label of me to startLabel
            set the backgroundcolor of me to red
      end if
end mouseUp

on DoCountUp
   put "Start Clock" into startLabel
   put "Stop Clock" into stopLabel
   
   if the label of ME is stopLabel then
      set the numberformat to 00
      add 1 to field GameClockSec
      if field GameClockSec = 60 then
         add 1 to field GameClockMin
         put 00 into field GameClockSec
      else
      end if
      send "DoCountUp tNewTime" to me in 1 second
   end if
end DoCountUp
Craig Newman

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Simple count up timer

Post by croivo » Mon Apr 13, 2015 9:21 pm

Thanks for try...
But clock still goes crazy after few fast clicks...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Simple count up timer

Post by dunbarx » Mon Apr 13, 2015 10:26 pm

Try raising the "30" to "60".

Craig

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Simple count up timer

Post by Newbie4 » Tue Apr 14, 2015 12:45 pm

Another approach is to cancel the last DoCountup message that you sent. In your original code, add the line

Code: Select all

       cancel item 1 of last line of the pendingMessages
for example, in your code:

Code: Select all

local holdTime
on mouseUp
      put "Start Clock" into startLabel
      put "Stop Clock" into stopLabel
      
      if the label of me is startLabel then
            set the label of me to stopLabel
            set the backgroundcolor of me to green
      put the ticks into holdTime -- NEW
            DoCountUp
      else
            set the label of me to startLabel
            set the backgroundcolor of me to red
            cancel item 1 of last line of the pendingMessages   ---NEW---
      end if
end mouseUp
That should prevent multiple "DoCountUp" messages being sent
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Simple count up timer

Post by croivo » Thu Apr 16, 2015 10:14 pm

That's it! :D Thanks very much

Post Reply