O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time Delays

There are times when you’ll want to be able to delay the program a bit before going on, or maybe you’ll want to execute the same command every minute. For complete details, see Chapter 15 and Chapter 17. To have the program sleep for x number of milliseconds, call after with the number of milliseconds:

$widget->after(milliseconds);

You almost never want to do this, as the event loop is blocked.

To specify a callback that will be called after so many milliseconds instead of waiting, send a callback as the second argument to after:

$id = $widget->after(milliseconds, callback);
# i.e.
$id = $widget->after(1000, \&do_something);

If you want to execute a subroutine after the program has been idle for a while, call afterIdle:

$id = $widget->afterIdle(callback);

To cancel the call to after or afterIdle, use afterCancel with the $id returned by after:

$widget->afterCancel($id);
# You can also do this:
$id->cancel;

You can have the program repeatedly call the same callback by using the repeat method:

$widget->repeat(milliseconds, callback);
# i.e.
$widget->repeat(600, \&update_status);

If you destroy $widget, any calls to after and repeat are automatically canceled for you.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required