Page 1 of 1
Beginner Stop Watch!
Posted: Fri Feb 22, 2013 2:00 am
by nalexander50
Hello,
I am new to LiveCode but have basic experience writing in languages like C, Java, and VB. Currently, I am trying to implement a timer into an application that I am writing but am having trouble. I want the application to Count up every second just like a stop watch. I also want it to displace hours, minutes, and seconds into respective slots fitting the format 00:00:00. I have been very close at implementing the timer, but am having serious troubles with the hours place. Can anyone help?
Re: Beginner Stop Watch!
Posted: Fri Feb 22, 2013 2:42 am
by Simon
Hello and welcome to the forum!
This should get you started:
Code: Select all
local tSec,tMin,tHour
on mouseUp
startClock
end mouseUp
on startClock
set the numberFormat to "00" --this sets it so that it always shows a leading 0
add 1 to tSec
if tSec = 60 then
add 1 to tMin
put 00 into tSec
end if
if tMin = 60 then
add 1 to tHour
put 00 into tMin
end if
if tMin = 0 then put 00 into tMin
if tHour = 0 then put 00 into tHour
put tHour & ":" & tMin & ":" & tSec into fld 1
send startClock to me in 1 sec --go back to the top and do it again in 1 sec
end startClock
I don't claim that this is the best way to do it. I'm sure that somebody here could write it with much less code. But I think this is easy to read.
Good luck and enjoy LiveCode!
Simon
Re: Beginner Stop Watch!
Posted: Thu Feb 28, 2013 10:52 pm
by nalexander50
Thanks so much. This is exactly what I needed. And I do not mind the length of code. I had this exact script set up, the only problem I was encountering was when I reached 1 hour, the seconds either set to 00 and stopped counting or was set to -36,000. I had a few bizarre bugs, but this is exactly what I needed. Thanks!
Re: Beginner Stop Watch!
Posted: Thu Feb 28, 2013 11:09 pm
by Simon
Glad I could help
Simon
Re: Beginner Stop Watch!
Posted: Fri Mar 01, 2013 5:59 am
by rumplestiltskin
Just dropped in to appreciate the code provided in this thread and was wondering something:
I figured out how to start and stop the timer using a flag I set when a mouseUp occurs in the field. How, though, would I reset the timer to zero? I am guessing I'd have to put the handler to do this in the same script in order to reset tSec, tMin and tHour to "00".
...time passes...
Yep; that did it. I added a button named "Reset" which sent a resetTimer message to the field and, in the resetTimer handler in the field, I set the three time vars to "00" and put empty into the field.
If I tried to reset tSec, etc. to "00" outside of the field script, it did nothing (as the variables declared as "local" can only be changed from any handler within
that script).
Thanks to the OP for asking the question and to Simon for his answer.

Re: Beginner Stop Watch!
Posted: Fri Mar 01, 2013 6:45 am
by jacque
EDIT: I just realized we're writing a stopwatch, not a clock, so the following isn't what you wanted. I'll leave it here anyway. This one is just a clock.
The convert command is handy for dates and times. Here's another way to do it:
Code: Select all
local sFlag
on clock
get the seconds
add 1 to it
convert it to long time
put it into fld 1
if sFlag is true then send "clock" to me in 1 second
end clock
Add a handler that sets sFlag to true or false to turn the clock on and off.
Re: Beginner Stop Watch!
Posted: Fri Mar 01, 2013 2:11 pm
by Dixie
Another way to do it... the script is in the card script..
be well
Dixie
Re: Beginner Stop Watch!
Posted: Fri Feb 07, 2014 3:48 am
by Mag
jacque wrote:EDIT: I just realized we're writing a stopwatch, not a clock, so the following isn't what you wanted. I'll leave it here anyway. This one is just a clock.
The convert command is handy for dates and times. Here's another way to do it:
Code: Select all
local sFlag
on clock
get the seconds
add 1 to it
convert it to long time
put it into fld 1
if sFlag is true then send "clock" to me in 1 second
end clock
Add a handler that sets sFlag to true or false to turn the clock on and off.
Hi Jacque,
Why you add 1 to it?
Re: Beginner Stop Watch!
Posted: Fri Feb 07, 2014 6:56 am
by jacque
The handler shouldn't add 1, I probably forgot to remove that line when I was copying from something else. Good catch.
Re: Beginner Stop Watch!
Posted: Fri Feb 07, 2014 2:14 pm
by Mag
Thanks!
Your message contains 7 characters. The minimum number of characters you need to enter is 10.
About Clock
Posted: Thu Feb 13, 2014 12:21 pm
by Mag
One last consideration: if the clock shows also the seconds, they do not coincide with those of the system, but their change is always a little later (it depends on when I start the script). I guess you can't make them change together with those of the system clock...
Code: Select all
on updateClock
put the long system time into field "ClockField"
send "updateClock" to me in 1 seconds
end updateClock
Re: Beginner Stop Watch!
Posted: Thu Feb 13, 2014 12:36 pm
by bn
Hi Mag,
if you want to sync your seconds to the seconds of the system you use a little different way to send in time
you calculate the time remaining to the next full second of the system and adjusting your send in time value accordingly.
Like this
Code: Select all
local sFlag = false
on mouseUp
put not sFlag into sFlag
send "doClock" to me in 0 milliseconds
end mouseUp
on doClock
if not sFlag then exit doClock
get the seconds
convert it to long time
put it into field 1
-- get remaining milliseconds
put 1000 - char 3 to - 1 of the milliseconds into tHowMany -- char 3 to - 1 of the milliseconds are the hundreds
send "doClock" to me in tHowMany milliseconds
end doClock
KInd regards
Bernd
Re: Beginner Stop Watch!
Posted: Thu Feb 13, 2014 6:33 pm
by jacque
One of the cleverest things I ever saw was a method from Geoff Canyon about how to calculate the exact moment the system clock changes the seconds:
Code: Select all
send "updateTimer" to me in (1 - (the long seconds mod 1)) seconds
Pretty cool.
Re: Beginner Stop Watch!
Posted: Thu Feb 13, 2014 6:43 pm
by Mag
Thanks guys, two ways to do one thing well!