Home > Archive > Tcl > June 2007 > Down counter
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I want to display a down counter in days,hours, minutes (& maybe
seconds).
It's fairly easy to calculate the 3 or 4 parameters I require:
e.g.
set end [clock scan "September 1, 2007"]
set start [clock scan now]
set diff [expr $end -$start]
set secs [expr $diff - 60*($diff/60)]
set mins [expr ($diff - (($days*86400)+($hours*3600)))/60]
set hours [expr ($diff - ($days*86400))/3600]
set days [expr ($diff/86400)]
but how do I recalculate every so often (half second or 10 second
intervals) and then update the display.
I tried to loop the above with an "after 500" statement, but
everything locked up.
Can I use a trace command on, say, the secs variable?
| |
| Helmut Giese 2007-06-27, 7:12 pm |
| On Wed, 27 Jun 2007 06:06:11 -0700, Niv <kev.parsons@mbda.co.uk>
wrote:
>but how do I recalculate every so often (half second or 10 second
>intervals) and then update the display.
Hi Niv,
put the code which updates your display into a proc and use the nice
'every' idiom from http://wiki.tcl.tk/9299.
HTH
Helmut Giese
| |
|
| On 27 Jun, 14:06, Niv <kev.pars...@mbda.co.uk> wrote:
> I want to display a down counter in days,hours, minutes (& maybe
> seconds).
> It's fairly easy to calculate the 3 or 4 parameters I require:
>
> e.g.
>
> set end [clock scan "September 1, 2007"]
> set start [clock scan now]
> set diff [expr $end -$start]
> set secs [expr $diff - 60*($diff/60)]
> set mins [expr ($diff - (($days*86400)+($hours*3600)))/60]
> set hours [expr ($diff - ($days*86400))/3600]
> set days [expr ($diff/86400)]
>
> but how do I recalculate every so often (half second or 10 second
> intervals) and then update the display.
>
> I tried to loop the above with an "after 500" statement, but
> everything locked up.
>
> Can I use a trace command on, say, the secs variable?
I've pasted the set "..." in the wrong order from my code, so ignore
that error please.
KP.
| |
| Glenn Jackman 2007-06-27, 7:12 pm |
| At 2007-06-27 09:06AM, "Niv" wrote:
> I want to display a down counter in days,hours, minutes (& maybe
> seconds).
> It's fairly easy to calculate the 3 or 4 parameters I require:
>
>
> e.g.
>
> set end [clock scan "September 1, 2007"]
> set start [clock scan now]
> set diff [expr $end -$start]
> set secs [expr $diff - 60*($diff/60)]
> set mins [expr ($diff - (($days*86400)+($hours*3600)))/60]
> set hours [expr ($diff - ($days*86400))/3600]
> set days [expr ($diff/86400)]
>
>
> but how do I recalculate every so often (half second or 10 second
> intervals) and then update the display.
>
> I tried to loop the above with an "after 500" statement, but
> everything locked up.
>
> Can I use a trace command on, say, the secs variable?
Have a look at http://wiki.tcl.tk/1011, particularly RS's
"every" proc for his digital clock in 3 lines of tcl.
Here's another way to calculate all the time params:
set ::end [clock scan "September 1, 2007"]
proc set_time_params {} {
global end days hours mins secs
set now [clock seconds]
set diff [expr {$end - $now}]
if {$diff < 0} {set diff 0}
set params [clock format $diff -format {%j %H %M %S} -gmt yes]
scan $params {%d %d %d %d} days hours mins secs
incr days -1
# next line for debugging
puts "${days}d ${hours}H ${mins}M ${secs}S"
}
# then, using RS's every:
proc every {ms body} {
eval $body
after $ms [list every $ms $body]
}
every 1000 set_time_params
Note, for testing I like to be able to cancel the every "infinite loop":
proc every {ms body} {
eval $body
set ::every_afterid [after $ms [list every $ms $body]]
}
proc cancel_every {} {
after cancel $::every_afterid
}
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
|
|
|
|
|