Page 1 of 1

Simple count up timer

Posted: Mon Apr 13, 2015 6:16 pm
by croivo
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

Re: Simple count up timer

Posted: Mon Apr 13, 2015 7:50 pm
by dunbarx
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

Re: Simple count up timer

Posted: Mon Apr 13, 2015 9:21 pm
by croivo
Thanks for try...
But clock still goes crazy after few fast clicks...

Re: Simple count up timer

Posted: Mon Apr 13, 2015 10:26 pm
by dunbarx
Try raising the "30" to "60".

Craig

Re: Simple count up timer

Posted: Tue Apr 14, 2015 12:45 pm
by Newbie4
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

Re: Simple count up timer

Posted: Thu Apr 16, 2015 10:14 pm
by croivo
That's it! :D Thanks very much