The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

UniEvent::Timer - schedule callbacks to be invoked in future

SYNOPSIS

    use UniEvent::Timer;

    my $t = UE::timer 1.5, sub { ... };
    my $t = UE::timer_once 60, sub { ... };
    
    UE::Loop->default->run;

    my $t = UniEvent::Timer->new();
    $h->start(2.7, sub { ... });
    $h->again();
    $h->stop;

DESCRIPTION

The Timer handle is used, when needed to run piece of code in future. If the code needs to be run once, the one-shot interface of the timer is used, otherwise the repeatable interface methods should be invoked. In any case the same callback(s) will be invoked, independently from kind of timer.

The Timer handle is inherited from UniEvent::Handle.

METHODS

All methods of UniEvent::Handle also apply.

create($repeat, $callback, [$loop = default])

    my $timer = UniEvent::Timer->create(0.5, sub { say "hi" });

Creates and starts a timer. Alias for new($loop) + start($repeat, $callback).

create_once($initial, $callback, [$loop = default])

    my $timer = UniEvent::Timer->create_once(0.5, sub { say "hi" });

Creates and starts a one-shot timer. Alias for new($loop) + once($initial, $callback).

new([$loop = default])

Constructs new Timer handle and binds it to the specified event loop.

start($repeat, [$callback])

start($repeat, $initial, [$callback])

Starts the repeatable timer handle. The timer callback will fire first time after $initial amount of seconds, and then endlessly repeating firing after each $repeat seconds. Both values can be fractional.

In the first form $initial equals to $repeat.

Optionally it adds the $callback as ->event->add($callback).

stop()

Stops the timer handle.

once($initial, [$callback])

Starts the one-shot timer handle. The timer callback will fire time after $initial amount of seconds (can be fractional).

Optionally it adds the $callback as ->event->add($callback).

The $initial value should not be zero. It is unspecified what will happen in this case. If you want to execute code "a little later" (on next loop iteration, for example), use "delay($callback)" in UniEvent::Loop.

callback($sub)

event()

Callback signature:

    my $handle = shift; # the timer handle itself

See "EVENT CALLBACKS" in UniEvent

event_listener($delegate, [$weak])

Method on_timer will be called.

See "EVENT LISTENER" in UniEvent

call_now()

Immediately ivokes assigned callbacks and listeners in the caller context (i.e. not waiting loop run).

again()

Stop the timer, and if it is repeating restart it using the last $initial or $repeat as the timeout.

It's an error to call this method if timer has never been started.

May return error

repeat([$new_value])

Sets or gets the repeat interval value in seconds (may be fractional).

due_in()

Returns time in seconds remaining for timer to fire. Returns 0 if timer has expired.

Note that this value is relative to "now()" in UniEvent::Loop so it may be inaccurate in case if you were not returning to the loop for some time.

In such case call "update_time()" in UniEvent::Loop before calling due_in() to get accurate value.

pause()

Temporarily pause (stop) the timer. Does nothing if timer has not been started or has been paused/stopped.

resume()

Resumes paused timer. Next callback will be called in number of seconds that were remaining when pause() was called.

May return error if timer was not paused before. resume() can't be called after stop() even if there was preceeding call to pause().