HELP CREATE 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
gilar
Posts: 96
Joined: Sat Sep 26, 2015 12:59 pm

HELP CREATE TIMER

Post by gilar » Thu Oct 29, 2015 4:38 am

Helllo Everybody ....
I have trouble to create countdown and countup timer
I found a code by googling and I success apply this code into my Application, BUT
The code can't count down more than 24:00.
And I try to modify the code to count up, I have no luck too.

What I want is:
1. CountDown from more than 24:00. ex: 90:00 or more.
2. Count Up from 00:00 to 45:00 and then stop. Then When I resume, The count up continue to 90:00 and then stop. ( I use this for soccer timer).

Any body could give me a guidance to solve my problem?
Comment and help would be appreciate.

thanx

Here is the code

Code: Select all

on mouseUp
   If the label of ME is "PLAY" then
      set the label of ME to "PAUSE"
      set the backgroundcolor of ME to green
else 
   set the label ME to "PLAY"
   set the backgroundcolor of ME to empty
end if
   DoCountDown field TIMER_fld
end mouseUp

on DoCountDown pTime
   if label of me is "PLAY"
else
   convert pTime to seconds 
   subtract 1 from pTime 
   convert pTime to dateItems 
   split pTime by "," 
   put format("%02d:%02d",pTime[4],pTime[5],pTime[6]) into tNewTime 
   put tNewTime into fld "TIMER_fld" 
   //CASPAR
   if tNewTime = "00:00" then do TimesUp
   else
      send "DoCountDown tNewTime" to me in 1 second
   end if
end if
end DoCountDown 

on TimesUp 
   //CASPAR
end TimesUp

on TimesStopped

end TimesStopped

Gilar | from INDONESIA

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

Re: HELP CREATE TIMER

Post by dunbarx » Thu Oct 29, 2015 5:14 am

Hi.

Not sure how accurate your counter has to be, but at a simple level, do this. Make a button and a field on a new card. Put this in the button script:

Code: Select all

on mouseUp
   ask "Enter Start time MM:SS" with "10:10"
   countDown it
end mouseUp

On countDown var
   put var into fld 1
   set the itemDel to ":"
   
   if the optionKey is down then exit countDown
   subtract 1 from item 2 of var
   if item 2 of var = 0 then
      put 60 into item 2 of var
      subtract 1 from item 1 of var
   end if
   send "countDown" && var to me in 60
end countDown
This may need a bit of polishing. But does it do what you need? Can you make the count-up version?

Craig Newman

gilar
Posts: 96
Joined: Sat Sep 26, 2015 12:59 pm

Re: HELP CREATE TIMER

Post by gilar » Thu Oct 29, 2015 6:05 am

dunbarx wrote:Hi.
Not sure how accurate your counter has to be, but at a simple level, do this. Make a button and a field on a new card. Put this in the button script:
This may need a bit of polishing. But does it do what you need? Can you make the count-up version?
Craig Newman
Hi Craig ... Thank You ....
but, when I put 45:00 then the timer be like this 45:-1, it should be 45:01, 45:02, 45:03 etc.

Now, I m trying to modify your code ...

btw, do you know about this code? ("%02d:%02d",pTime[4],pTime[5],pTime[6])
I'm trying to google it but i did not find something explaining this code.
Gilar | from INDONESIA

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

Re: HELP CREATE TIMER

Post by dunbarx » Thu Oct 29, 2015 1:55 pm

Hi.
but, when I put 45:00 then the timer be like this 45:-1, it should be 45:01, 45:02, 45:03 etc.
Of course it does. Do you see why? The code I gave you "needs polishing", remember? I hope you will experiment with it, so you can fix the issue with a "00" starting value in item 2, fix what happens when it gets to "00:00", and whatever else you find while testing, That is how you make software.

You mentioned above that you wanted to count up. I only made the countDown version. But the sample I gave you ought to lead you there as well.

Write back if you get stuck, but only if you get stuck. It is your job to learn the basics of LiveCode, and to slog through the process of writing your application. Hopefully that will be fun and rewarding.

Craig

gilar
Posts: 96
Joined: Sat Sep 26, 2015 12:59 pm

Re: HELP CREATE TIMER

Post by gilar » Sat Oct 31, 2015 1:49 am

Hi Craig ... I found this thread http://forums.livecode.com/viewtopic.php?f=7&t=24014

Croivo create timer with 2 text field
- GameClockSec
- GameClockMin

then I modify the timer code into 1 text field
-GameClock
here the 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 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
            DoCountDown
      else
            set the label of me to startLabel
            set the backgroundcolor of me to red
            cancel item 1 of last line of the pendingMessages
      end if
end mouseUp

on DoCountDown
   put "Start Clock" into startLabel
   put "Stop Clock" into stopLabel
   if the label of ME is stopLabel then
      set the numberformat to 00
      subtract 1 from char 4 to 5  of word 1 of field GameClock
      if  char 4 to 5 of word 1 of field GameClock = 00 then
         subtract 1 from char 1 to 2 of word 1 of field GameClock
         wait 1 seconds
         put 59 into char 4 to 5 of word 1 of field GameClock
      else
      end if
      send "DoCountDown tNewTime" to me in 1 second
   end if
end DoCountDown
Now the the problem is ...
I wan to stop timer at 00:00, but I have no Luck,

I'm trying to do Times Up with this 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 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
   else
      set the label of me to startLabel
      set the backgroundcolor of me to red
      cancel item 1 of last line of the pendingMessages
   end if
   DoCountDown field GameClock
end mouseUp

on DoCountDown
   put "Start Clock" into startLabel
   put "Stop Clock" into stopLabel
   if the label of ME is stopLabel then
      set the numberformat to 00
      subtract 1 from char 4 to 5  of word 1 of field GameClock
      if  char 4 to 5 of word 1 of field GameClock = 00 then
         subtract 1 from char 1 to 2 of word 1 of field GameClock
         wait 1 second
         put 59 into char 4 to 5 of word 1 of field GameClock
         if  field GameClock = "00:00" then do TimesUp
         else
            send "DoCountDown tNewTime" to me in 1 second
         end if
      end if
   end if
end DoCountDown

on TimesUp
   
end TimesUp

on TimesStopped

end TimesStopped
Any ideas?

Thank
Gilar
Gilar | from INDONESIA

gilar
Posts: 96
Joined: Sat Sep 26, 2015 12:59 pm

Re: HELP CREATE TIMER

Post by gilar » Sat Oct 31, 2015 2:01 am

Btw, I google again this code ("%02d:%02d",pTime[4],pTime[5],pTime[6])
I found this page http://runtime-revolution.278305.n4.nab ... 79798.html
I think i found you too there ... Craig :) :) :)
this is about posix time, the code that I have written was incomplete.
The code shouldbe like this: ("%02d:%02d:%02d",tTime[4],tTime[5],tTime[6])
and 24 is belong to hours. That why i cannot change the value into more than 23:59.
Gilar | from INDONESIA

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

Re: HELP CREATE TIMER

Post by dunbarx » Sat Oct 31, 2015 3:53 am

Hi.

OK. This is sloppy, and you may want to play with the field formatting, but try it. Make a button and a field. In the button script:

Code: Select all

on mouseUp
      ask "Enter Start time MM:SS" with "0:10"
end mouseUp

On countDown var
    if the optionKey is down then exit countDown
      set the itemDel to ":"
      put var into fld 1
   switch
      case  var = "0:0"
         countUp var
         break
      case item 2 of var = 0 and item 1 of var <> 0
         put 60 into item 2 of var
         subtract 1 from item 1 of var
         break
   end switch
           subtract 1 from item 2 of var
      send "countDown" && var to me in 60
end countDown

On countUp var
    if the optionKey is down then exit countUp
      set the itemDel to ":"
      put var into fld 1
   switch
      case item 2 of var = 60
         put 0 into item 2 of var
         add 1 to item 1 of var
         break
      case item 2 of var <> 60
         add 1 to item 2 of var
         break
   end switch
   wait 60
   countUp var
end countUp
Craig

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: HELP CREATE TIMER

Post by townsend » Sat Oct 31, 2015 8:34 pm

Here's one I think you'll like. It's a COUNT-UP timer. You can set the count-up time in the code. Or add a field or ASK command if you need to use different count-up times. Also, this code be modified to be a count-down timer.

Code: Select all

-- count-up seconds timer v3, written by me
-- just drop this code into any button and click to start

local started, cntr

on mouseUp
     if the label of me <> "Start" then set the label of me to "Start"
     if the label of me = "Start" then
          put the long seconds into started
          put (the long seconds - started) into cntr
          set the label of me to cntr
          countUp
     else if value(the label of me) >0 then
          set the label of me to "Start"
     end if
end mouseUp

on countUp
     if cntr >10 then  -------> SET TIMER VALUE in SECONDS <-------
          set the label of me to "Start"
          exit to top
     else 
          if the label of me = "Start" then
               exit to top
          else
               put (the long seconds - started) into cntr
               set the label of me to cntr
          end if
     end if
     -- breakpoint
     send "countUp" to me in 0.2 seconds
end countUp
Last edited by townsend on Sat Oct 31, 2015 9:44 pm, edited 2 times in total.

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: HELP CREATE TIMER

Post by townsend » Sat Oct 31, 2015 9:10 pm

Okay. Here's the count-down version. Notice here, instead of using 'the long seconds' I just used 'the seconds' so all those fractional seconds are not displayed.

Code: Select all

-- seconds time v3, written by me
-- just drop this code into any button and click to start

local started, cntr

on mouseUp
     if the label of me <> "Start" then set the label of me to "Start"
     if the label of me = "Start" then
          put the seconds into started
          put (started+10) into cntr -------> SET TIMER VALUE in SECONDS <-------
          set the label of me to cntr - the seconds
          countUp
     else if value(the label of me) <0 then
          set the label of me to "Start"
     end if
end mouseUp

on countUp
     if cntr - the seconds <= 0 then  
          set the label of me to "Start"
          exit to top
     else 
          if the label of me = "Start" then
               exit to top
          else
               --put (the seconds - started) into cntr
               set the label of me to cntr - the seconds
          end if
     end if
     -- breakpoint
     send "countUp" to me in 0.2 seconds
end countUp

gilar
Posts: 96
Joined: Sat Sep 26, 2015 12:59 pm

Re: HELP CREATE TIMER

Post by gilar » Mon Nov 02, 2015 6:14 pm

Hi ... townsend
Thank You ....

It's very nice ... Now, I'm going to modify your code,
- I'll put the timer into text filed (not int button label), and I'm gonna create another Button PLAY/PAUSE button to control timer on the Text field.
- I'll Change the timer format into mm:ss, and the timer must be count up more than 60 minute, because I'm going to use this timer for Soccer games.
- I'll Change the timer into Global timer, it's means if I have a lot of card on a stack. the timer will still running when i'M going into another Card or Substack.
- I hope I can change timer value to start in text field (Not on script). It means, when I wan to start timer from 45:00, I just type 45:00 ont text field (not on the script).

later I'll report my result here......

Thanx
Gilar | from INDONESIA

Post Reply