Countdown HH:MM:SS
Nonsanity
form at nonsanity.com
Wed Jul 20 15:51:50 EDT 2011
I wouldn't rely on the send in time structure to actually fire every second.
It's going to slip over time as the computer takes periodic spikes of use.
If you want New Years Eve accuracy of the final countdown, you should
probably re-calculate the delta based on the current time continuously.
Here's the code from a quick test stack's single button. There are also
three fields.
on mouseup
put the internet date into fld 1 -- display the current time
get fld 2 -- get the target time
convert it to seconds
set the targetTime of me to it
UpdateCountdown
end mouseup
on UpdateCountdown
get the targetTime of me
put CountdownCalc( it ) into fld 3 -- display the countdown
-- always best to make sure this timer loop hasn't already been started
if "UpdateDountdown" is not in the pendingmessages then send
"UpdateCountdown" to me in 1 second
end UpdateCountdown
function CountdownCalc targetTime -- in seconds
put targetTime - the seconds into deltaTime
put trunc(deltaTime / 3600) into h -- hours
put trunc((deltaTime - (h * 3600)) / 60) into m -- minutes
put deltaTime - (h * 3600) - (m * 60) into s -- seconds
put format( "%i:%02i:%02i", h, m, s ) into countdown
return countdown
end CountdownCalc
Pretty much the same as the other samples, but more accurate over the long
haul in those final seconds.
~ Chris Innanen
~ Nonsanity
On Wed, Jul 20, 2011 at 3:19 PM, Roger Eller <roger.e.eller at sealedair.com>wrote:
> On Wed, Jul 20, 2011 at 1:06 PM, Ken Ray wrote:
>
> > > Using the dateItems is definitely the right direction, but my objective
> > is
> > > to count backwards from for example; 2 hours, 45 minutes, and 59
> seconds
> > > while updating the display field every second. When the countdown
> > reaches
> > > 00:00:00, I would play a sound of do other actions.
> >
> > Sorry about that... this will work if you just want to count down from a
> > fixed amount of time:
> >
> > on mouseUp
> > DoCountDown "2:45:59"
> > end mouseUp
> >
> > on DoCountDown pTime
> > convert pTime to seconds
> > subtract 1 from pTime
> > convert pTime to dateItems
> > split pTime by ","
> > put format("%02d:%02d:%02d",pTime[4],pTime[5],pTime[6]) into tNewTime
> > put tNewTime into fld "Time"
> > if tNewTime = "00:00:00" then
> > TimesUp
> > else
> > send "DoCountDown tNewTime" to me in 1 second
> > end if
> > end DoCountDown
> >
> > on TimesUp
> > answer "Done"
> > end TimesUp
> >
> > Ken Ray
> > Sons of Thunder Software, Inc.
> > Email: kray at sonsothunder.com
> > Web Site: http://www.sonsothunder.com/
> >
> >
> With only a few tweaks like handling a cancel/reset request, this script
> does what I need. James' was almost the one, but the time wasn't starting
> from a pre-set amount of time such as 1 hour, etc. Thanks to everyone that
> contributed!
>
> ~Roger
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
More information about the use-livecode
mailing list