The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

IO::Async::Timer - event callback after some timed delay

SYNOPSIS

 use IO::Async::Timer;

 use IO::Async::Loop;
 my $loop = IO::Async::Loop->new();

 my $timer = IO::Async::Timer->new(
    mode => "countdown",
    delay => 10,

    on_expire => sub {
       print "Sorry, your time's up\n";
       $loop->loop_stop;
    },
 );

 $loop->add( $timer );

 $loop->loop_forever;

DESCRIPTION

This module provides a class of IO::Async::Notifier for implementing timed delays. A Timer object implements a countdown timer, which invokes its callback after the given period from when it was started. After it has expired the Timer may be started again, when it will wait the same period then invoke the callback again. A timer that is currently running may be stopped or reset.

This object may be used in one of two ways; with a callback function, or as a base class.

Callbacks

If the on_expire key is supplied to the constructor, it should contain a CODE reference to a callback function to be invoked at the appropriate time:

 $on_expire->( $self )
Base Class

If a subclass is built, then it can override the on_expire method.

 $self->on_expire()

PARAMETERS

The following named parameters may be passed to new or configure:

mode => STRING

The type of timer to create. Currently the only allowed mode is countdown but more types may be added in the future. Can only be given at construction time.

on_expire => CODE

CODE reference to callback to invoke when the timer expires. If not supplied, the subclass method will be called instead.

delay => NUM

The delay in seconds after starting the timer until it expires. Cannot be changed if the timer is running.

Once constructed, the Timer will need to be added to the Loop before it will work. It will also need to be started by the start method.

METHODS

$running = $timer->is_running

Returns true if the Timer has been started, and has not yet expired, or been stopped.

$timer->start

Starts the Timer. Throws an error if it was already running.

If the Timer is not yet in a Loop, the actual start will be deferred until it is added. Once added, it will be running, and will expire at the given duration after the time it was added.

$timer->stop

Stops the Timer if it is running.

$timer->reset

If the timer is running, restart the countdown period from now. If the timer is not running, this method has no effect.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>