Book HomeMastering Perl/TkSearch this book

13.22. 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, "Anatomy of the MainLoop" and Chapter 17, "Images and Animations". 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.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.