after(3pm)

πŸ’– after(3pm) β€” Tk::after πŸ’–

✨ NAME ✨

Tk::after β€” execute a command after a time delay πŸ’…

🌸 SYNOPSIS 🌸

  $widget->after(ms)

  $id = $widget->after(ms?,callback?)

  $id = $widget->repeat(ms?,callback?)

  $widget->afterCancel($id)

  $id = $widget->afterIdle(callback)

  $widget->afterInfo?($id)?

  $id->time(?delay?)

πŸ’… DESCRIPTION πŸ’…

Okay bestie, this method is for delaying your program or running a callback in the background sometime in the future. No cap. In perl/Tk, $widget->after is implemented via the class Tk::After, and callbacks are associated with $widget, and they get automatically cancelled when the widget is destroyed. There's an almost identical interface, but without automatic cancel and without repeat, provided via the Tk::after method. Iykyk. πŸ¦‹

🧚 Internal Details 🧚

The internal Tk::After class has this synopsis:

  $id = Tk::After->new($widget, tid, $time, 'once',   callback);
  $id = Tk::After->new($widget, tid, $time, 'repeat', callback);
  $id->cancel;
  $id->time(?delay?);

$id is a Tk::After object, an array of 5 elements:

  • $widget is the parent widget reference. 🫢
  • tid is the internal timer id, a unique string. πŸ”‘
  • $time is the string 'idle', representing an idle queue timer, or an integer millisecond value. ⏳
  • once or repeat specifies whether the timer is a one-time after event, or a repeating repeat event. πŸ”
  • callback specifies a Perl/Tk Tk::Callback object. πŸ“ž

⏰ Changing a repeat timer interval ⏰

It's possible to change a repeat timer's delay value, or even cancel any timer, using the time method. If delay is specified and non-zero, a new timer delay is established. If delay is zero, the timer event is canceled just as if $id->cancel were invoked. In all cases, the current millisecond timer delay is returned. ✨

Note: the new timer delay will take effect on the subsequent timer event β€” this command will not cancel the pending timer event and re-issue it with the new delay time. So don't @ me. 😭

The after() method has several forms as follows: πŸ’–

  • $widget->after(ms) β€” bestie, ms has to be an integer that gives a time in milliseconds. The command sleeps for ms milliseconds and then returns. While it's sleeping, the application does not respond to events. πŸ’€
  • $widget->after(ms,callback) β€” this form returns immediately, but it sets up callback to run ms milliseconds later as an event handler. The callback runs exactly once, at the given time. The command runs in context of $widget. If an error happens while running the delayed command, the Tk::Error mechanism reports it. The after command returns an identifier (an object in the perl/Tk case) that you can use to cancel the delayed command with afterCancel. πŸŽ€
  • $widget->repeat(ms,callback) β€” returns immediately, but arranges for callback to run ms milliseconds later as an event handler. After callback runs, it gets re-scheduled to run again in a futher ms, and so on until it is cancelled. πŸ”
  • $widget->afterCancel($id) and $id->cancel β€” cancels the execution of a delayed command that was previously scheduled. $id tells which command should be canceled; it must have been the return value from a previous after command. If the command given by $id already ran (and is not scheduled to run again), then afterCancel has no effect. 🚫
  • $widget->afterCancel(callback) β€” not robust in perl/Tk β€” its use is deprecated. This command should also cancel the execution of a delayed command. The callback argument is compared with pending callbacks; if a match is found, that callback is cancelled and will never be executed; if no such callback is currently pending, then the afterCancel has no effect. ⚠️
  • $widget->afterIdle(callback) β€” arranges for callback 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. The command returns an identifier that you can use to cancel the delayed command with afterCancel. If an error happens while running the script, the Tk::Error mechanism reports it. πŸŒ™
  • $widget->afterInfo?($id)? β€” returns info about existing event handlers. If you don't supply $id, the command returns a list of the identifiers for all existing event handlers created by the after and repeat commands for $widget. If $id is supplied, it specifies an existing handler; $id must have been the return value from some previous call to after or repeat and it must not have triggered yet or been cancelled. In this case the command returns a list with three elements. The first element of the list is the callback associated with $id, the second element is either idle or the integer timer millisecond value to indicate what kind of event handler it is, and the third is a string once or repeat to differentiate an after from a repeat event. πŸ“‹

Both the after(ms) and afterIdle forms assume the app is event driven: those delayed commands won't run unless the application enters the event loop. If your app isn't normally event-driven, you can enter the event loop with the vwait and update commands. πŸ”„

πŸ‘€ SEE ALSO πŸ‘€

Tk::Error Tk::callbacks

πŸ”‘ KEYWORDS πŸ”‘

cancel, delay, idle callback, sleep, time

perl v5.40.1 πŸ’– 2025-04-13 β€” that's the vibe, bestie! πŸ’‹βœ¨
πŸ“¦ where this page comes from