after(3tcl)
π after β bestie's guide to "do this thing later" π
okay so this is after, a Tcl Built-In Command, and it's literally the "wait a sec bestie" of the Tcl world. it delays stuff, or runs stuff in the background later. main character energy fr fr. πΈ
β¨ SYNOPSIS β¨
after ms
after ms ?script script script ...?
after cancel id
after cancel script script script ...
after idle ?script script script ...?
after info ?id?
π¦ DESCRIPTION π¦
so this command does one of two things: it delays execution of the program, or it executes a command in the background sometime in the future. it's giving "not right now but soon." it has several forms and which one you get depends on the first argument, so let's go one by one π
after msβmshas to be an integer giving a time in milliseconds. a negative number gets treated as0. the command sleeps formsmilliseconds and then returns. and here's the tea: while the command is sleeping, the application does not respond to events. like at all. it's fully checked out. πafter ms ?script script script ...?β this form returns immediately, but it sets up a Tcl command to runmsmilliseconds later as an event handler. the command gets executed exactly once, at the given time. the delayed command is made by concatenating all thescriptarguments in the same fashion as theconcatcommand. it runs at global level (so outside the context of any Tcl procedure, no sneaky local vars). if an error happens while the delayed command runs, then the background error gets reported by the command registered withinterp bgerror. theaftercommand returns an identifier you can use to cancel it withafter cancel. and amsvalue of0(or negative) queues the event immediately with priority over other event types β unless it was installed with an event proc, in which case it'll wait for the next round of events. periodt.after cancel idβ cancels the execution of a delayed command that was scheduled earlier.idsays which command to cancel, and it must have been the return value from a previousaftercommand. if the command given byidhas already been executed, thenafter canceljust... has no effect. no drama, nothing happens. πafter cancel script script ...β yep, this one also cancels a delayed command. thescriptarguments get concatenated together with space separators (just as in theconcatcommand). if there's a pending command that matches the string, it's canceled and will never be executed. if no such command is currently pending, thenafter cancelhas no effect. that's it, that's the whole vibe.after idle script ?script script ...?β concatenates thescriptarguments together with space separators (just as in theconcatcommand), and arranges for the resulting script to be evaluated later as an idle callback. the script runs exactly once, the next time the event loop is entered and there are no events to process. it returns an identifier you can use to cancel it withafter cancel. if an error occurs while executing the script, then the background error gets reported by the command registered withinterp bgerror. π§after info ?id?β this one is the gossip girl of the group: it returns information about existing event handlers. if noidargument is supplied, it returns a list of the identifiers for all existing event handlers created by theaftercommand for this interpreter. ifidis supplied, it specifies an existing handler βidmust have been the return value from some previous call toafter, and it must not have triggered yet or been canceled. in that case the command returns a list with two elements: the first element is the script associated withid, and the second element is eitheridleortimerto indicate what kind of event handler it is. iconic. π
heads up bestie: theafter msandafter idleforms assume that the application is event driven. the delayed commands will not be executed unless the application enters the event loop. in applications that aren't normally event-driven, liketclsh, you can enter the event loop with thevwaitandupdatecommands. iykyk. π«Ά
π EXAMPLES π
this one defines a command to make Tcl do nothing at all for N seconds:
proc sleep {N} {
after [expr {int($N * 1000)}]
}
this arranges for the command wake_up to be run in eight hours (providing the event loop is active at that time):
after [expr {1000 * 60 * 60 * 8}] wake_up
and the following command can be used to do long-running calculations (as represented here by ::my_calc::one_step, which is assumed to return a boolean indicating whether another step should be performed) in a step-by-step fashion β though the calculation itself needs to be arranged so it can work step-wise. this technique is extra careful to make sure the event loop isn't starved by the rescheduling of processing steps (it arranges for the next step to be done using an already-triggered timer event only when the event queue has been drained), and it's super useful when you want to make sure a Tk GUI stays responsive during a slow task. queen behaviour. π
proc doOneStep {} {
if {[::my_calc::one_step]} {
after idle [list after 0 doOneStep]
}
}
doOneStep
π SEE ALSO π
concat(3tcl), interp(3tcl), update(3tcl), vwait(3tcl)
π KEYWORDS π
cancel, delay, idle callback, sleep, time
and that's the whole page bestie!! this one comes from the Tcl Built-In Commands manual, Tcl version 7.5, after(3tcl) πβ¨ππ¦ go schedule something and slay π«Ά
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1990-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms