timer(n)

๐Ÿ“ฆ version 9.1.0, in nixpkgs unstable

๐Ÿ’… timer(n) โ€” Tcl Built-In Commands, but make it cute

okay bestie, welcome to timer(n), the Tcl built-in that handles timer events for monotonic and wall clock time โœจ. It's giving "I schedule things and I also make you wait" energy, and honestly? Iconic.


๐ŸŒธ NAME ๐ŸŒธ

timer โ€” timer events for monotonic and wall clock time. that's the whole vibe. ๐Ÿ•ฐ๏ธ


โœจ SYNOPSIS โœจ

timer in delay unit script
timer at timepoint unit script
timer idle script
timer sleep for delay ?unit?
timer sleep until timepoint ?unit?
timer cancel id
timer info ?id?

seven forms, seven little moods. slay. ๐Ÿ’…


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so like, this command is here to delay execution of your program or to run a command in the background sometime in the future ๐Ÿซถ. time can be specified two ways, bestie: by a monotonic time delay, or by a wall clock time point. and it has the following methods, presented in order because we're not chaotic (we are, but we're pretending) ๐ŸŽ€

๐Ÿงš timer in delay unit script

okay so in this form the command returns immediately โ€” like, it does not wait around, it has places to be โ€” but it arranges for a Tcl command to be executed after the given monotonic time delay has elapsed, as an event handler โณ. the delay value is given in the specified unit โ€” peep TIME UNITS below for the available units ๐Ÿ‘€.

the command will be executed exactly once, after the given time delay. the delayed command is given by the argument script. and heads up: it gets executed at global level (outside the context of any Tcl procedure) โ€” no hiding in locals, bestie ๐ŸŒ.

if an error occurs while executing the delayed command, then the background error will be reported by the command registered with interp bgerror ๐Ÿ˜ญ.

the command returns an identifier that you can use to cancel the delayed command with timer cancel or after cancel ๐ŸŽซ. keep it safe!

a delay value of 0 (or negative) queues the event immediately with priority over other event types โ€” unless it's installed with an event proc, in which case it'll wait for the next round of events ๐Ÿ™ˆ.

also, fun fact: the command after ms script may also be used to register monotonic clock timer events. they are handled by the same queue as this form of the timer command ๐Ÿ”ฅ one queue, one family, no cap.

๐ŸŒˆ timer at timepoint unit script

same as timer in, but you're specifying a wall clock time point (epoc) instead ๐Ÿ—“๏ธ. a typical use-case is to pass the result of a clock scan as the time point โ€” see the example below, iykyk ๐Ÿ’–.

wall clock events have second priority after monotonic clock events ๐Ÿ‘‘. if both fire the same instant, the monotonic clock command is always handled first. them's the rules.

๐Ÿ“ timer idle script

this one arranges for the script to be evaluated later as an idle callback ๐Ÿ˜ดโœจ. the script will be run exactly once, the next time the event loop is entered and there are no events to process โ€” so it's the "whenever you're free, babe" option.

the command returns an identifier that can be used to cancel the delayed command using timer cancel or after cancel ๐ŸŽซ. and if an error occurs while executing the script, the background error will be reported by the command registered with interp bgerror ๐Ÿ’€.

โญ timer sleep for delay ?unit?

delay must be a wide integer giving a time in the given unit (default: milliseconds). a negative number is treated as 0 ๐Ÿ™…โ€โ™€๏ธ.

the command sleeps for the given monotonic delay and then returns. while the command is sleeping, the application does not respond to events โ€” yes bestie, it's a full nap, nothing gets through ๐Ÿ˜ด.

๐ŸŒธ timer sleep until timepoint ?unit?

timepoint must be a wide integer giving a wall clock time point in the given unit (default: seconds). a negative number is treated as 0 ๐Ÿ™…โ€โ™€๏ธ.

the command sleeps (at least) until the given wall clock time point and then returns. while it's sleeping, the application does not respond to events โ€” again, full nap, do not disturb ๐Ÿ›Œ.

๐ŸŽ€ after cancel id

cancels the execution of a delayed command that was previously scheduled โŒ. id indicates which command should be canceled; it must have been the return value from a previous timer or after command. if the command given by id has already been executed, then the timer cancel command has no effect โ€” it's a no-op, not a crisis, we move ๐Ÿ’….

๐Ÿ’œ timer info ?id?

this command returns information about existing event handlers ๐Ÿ•ต๏ธโ€โ™€๏ธ.

if no id argument is supplied, the command returns a list of the identifiers for all existing event handlers created by the timer and after command for this interpreter ๐Ÿ“‹.

if id is supplied, it specifies an existing handler; id must have been the return value from some previous call to timer or after, and it must not have triggered yet or been canceled โš ๏ธ.

in that case the command returns a list with two to three elements:

  • the first element is the script associated with id ๐Ÿ“œ
  • the second element is either idle, monotonic or wallclock to indicate what kind of event handler it is ๐Ÿท๏ธ
  • a third list element is present for the kinds monotonic or wallclock. this element is the scheduled execution time point in microseconds of the given clock type โฑ๏ธ

okay so about timer in, timer at and timer idle โ€” these forms assume the application is event driven ๐ŸŒŠ. the delayed commands will not be executed unless the application enters the event loop. in applications that are not normally event-driven, such as tclsh, the event loop can be entered with the vwait and update commands ๐Ÿ‘ˆ write that down.

๐Ÿ“Œ note that the output differs from after info. different list, different day, don't @ me.

โฑ๏ธ TIME UNITS โฑ๏ธ

some forms of the command take a time unit, bestie. available units are:

  • us (for microseconds)
  • microseconds
  • ms (for milliseconds)
  • milliseconds
  • s (for seconds)
  • seconds

โ€ฆor any unique abbreviation ๐Ÿ˜Œ cute and flexible.


๐Ÿ”ฅ TIME MAXIMUM VALUE ๐Ÿ”ฅ

the arguments delay and timepoint are bound to a resulting 63 bit clock value (in unit microseconds) ๐Ÿงฎ.

any higher value (which is after year 294441) will result in a time too far away error. yes, that's a real year. no, you will not be alive for it. still, the limit is the limit ๐Ÿ’€.

๐ŸŒŸ EXAMPLES ๐ŸŒŸ

this arranges for the command wake_up to be run in eight hours wall clock time (providing the event loop is active at that time, bestie โ€” don't forget the event loop):

timer at [clock add [clock seconds] 8 hours] seconds wake_up

ate. no crumbs. ๐Ÿฝ๏ธ


๐Ÿซถ SEE ALSO ๐Ÿซถ

concat(n), interp(n), timer(n), update(n), vwait(n) โ€” go read them, they're all part of the friend group ๐Ÿ’–


๐Ÿ’ซ KEYWORDS ๐Ÿ’ซ

cancel, delay, idle callback, sleep, time


that's the whole page, bestie ๐ŸŽ€ โ€” signed off from Tcl 9.1, page timer(n), and yes the header said timer(n) / Tcl Built-In Commands at the top too. go forth and schedule responsibly ๐Ÿ’…โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • nixpkgs unstable: tcl 9.1.0
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms