Tcl after Command
last modified October 20, 2026
The Tcl after command schedules script execution after a time
delay. It's essential for creating timers and delayed actions in Tcl. The
delay is given in milliseconds, and the command can either pause the program or
schedule a script callback.
Basic Definition
The after command delays script execution or returns timer IDs.
A scheduled script runs once; a repeating event is made by having the script
schedule itself again. Scheduled scripts run from Tcl's event loop, at the
global level, so they only run while the program is processing events (for
example inside vwait, or in a Tk application).
Syntax: after ms ?script script ...?. With one argument, it pauses
execution. With a script, it schedules script execution after ms milliseconds
and returns an ID. Related forms are after cancel id,
after idle script and after info.
Simple Delay
This example shows the basic delay functionality of the after
command.
puts "Start" after 2000 puts "End"
This script prints "Start", waits 2000 milliseconds (2 seconds), then prints
"End". The after command with one argument pauses execution; it
blocks the whole program, and no events are processed while it sleeps.
Scheduled Script Execution
This demonstrates scheduling a script to run after a delay while the main script continues.
puts "Main script starts"
after 1000 {puts "Delayed message"}
puts "Main script continues"
vwait forever
The main script starts, schedules a message to appear after 1 second, then
continues. The vwait keeps the event loop running to process
the delayed script. The variable forever is never set, so the
script keeps waiting after the message appears and has to be stopped with
Ctrl+C (like all the examples below that end with vwait forever).
Canceling a Scheduled Event
The after cancel command cancels a previously scheduled event
using its ID.
set id [after 3000 {puts "This won't appear"}]
after 1000 {after cancel $id; puts "Event canceled"}
vwait forever
This schedules a message for 3 seconds but cancels it after 1 second. The
after cancel command uses the ID returned by the first
after; the ID can also be replaced by the script text itself.
Repeating Timer
This example creates a repeating timer by having the callback reschedule itself.
proc repeat {} {
puts "Tick [clock format [clock seconds] -format %T]"
after 1000 repeat
}
repeat
vwait forever
The repeat procedure prints the current time every second. Each
invocation schedules itself to run again after 1000 milliseconds. The delay
is counted from the end of the callback, so the ticks drift slightly later over
time.
Multiple Timers
This shows how to manage multiple independent timers with different intervals.
proc timer1 {} {
puts "Timer1: [clock seconds]"
after 1500 timer1
}
proc timer2 {} {
puts "Timer2: [clock seconds]"
after 2500 timer2
}
timer1
timer2
vwait forever
Two independent timers run with different intervals (1.5s and 2.5s). Each
timer maintains its own scheduling by calling after in its
callback.
After with Arguments
This demonstrates passing arguments to a callback procedure using after.
proc callback {msg count} {
puts "$msg $count"
incr count
after 1000 [list callback $msg $count]
}
callback "Count:" 1
vwait forever
The callback procedure receives and increments a counter. The
list command ensures proper argument passing when scheduling
the next execution.
Best Practices
- Units: Remember time is in milliseconds (1000ms = 1s).
- Event Loop: Use
vwaitto keep the event loop running. - Memory: Cancel timers when no longer needed.
- Arguments: Use
listfor callback arguments. - Blocking:
after mswithout a script freezes the program, including a GUI; use the callback form in event-driven code.
This tutorial covered the Tcl after command with practical
examples showing timer creation, delayed execution, and event scheduling.
Author
List all Tcl Tutorials.