EV

 view release on metacpan or  search on metacpan

libev/ev.pod  view on Meta::CPAN

=item ev_tstamp ev_timer_remaining (loop, ev_timer *)

Returns the remaining time until a timer fires. If the timer is active,
then this time is relative to the current event loop time, otherwise it's
the timeout value currently configured.

That is, after an C<ev_timer_set (w, 5, 7)>, C<ev_timer_remaining> returns
C<5>. When the timer is started and one second passes, C<ev_timer_remaining>
will return C<4>. When the timer expires and is restarted, it will return
roughly C<7> (likely slightly less as callback invocation takes some time,
too), and so on.

=item ev_tstamp repeat [read-write]

The current C<repeat> value. Will be used each time the watcher times out
or C<ev_timer_again> is called, and determines the next timeout (if any),
which is also when any modifications are taken into account.

=back

=head3 Examples

Example: Create a timer that fires after 60 seconds.

   static void
   one_minute_cb (struct ev_loop *loop, ev_timer *w, int revents)
   {
     .. one minute over, w is actually stopped right here
   }

   ev_timer mytimer;
   ev_timer_init (&mytimer, one_minute_cb, 60., 0.);
   ev_timer_start (loop, &mytimer);

Example: Create a timeout timer that times out after 10 seconds of
inactivity.

   static void
   timeout_cb (struct ev_loop *loop, ev_timer *w, int revents)
   {
     .. ten seconds without any activity
   }

   ev_timer mytimer;
   ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */
   ev_timer_again (&mytimer); /* start timer */
   ev_run (loop, 0);

   // and in some piece of code that gets executed on any "activity":
   // reset the timeout to start ticking again at 10 seconds
   ev_timer_again (&mytimer);


=head2 C<ev_periodic> - to cron or not to cron?

Periodic watchers are also timers of a kind, but they are very versatile
(and unfortunately a bit complex).

Unlike C<ev_timer>, periodic watchers are not based on real time (or
relative time, the physical time that passes) but on wall clock time
(absolute time, the thing you can read on your calendar or clock). The
difference is that wall clock time can run faster or slower than real
time, and time jumps are not uncommon (e.g. when you adjust your
wrist-watch).

You can tell a periodic watcher to trigger after some specific point
in time: for example, if you tell a periodic watcher to trigger "in 10
seconds" (by specifying e.g. C<ev_now () + 10.>, that is, an absolute time
not a delay) and then reset your system clock to January of the previous
year, then it will take a year or more to trigger the event (unlike an
C<ev_timer>, which would still trigger roughly 10 seconds after starting
it, as it uses a relative timeout).

C<ev_periodic> watchers can also be used to implement vastly more complex
timers, such as triggering an event on each "midnight, local time", or
other complicated rules. This cannot easily be done with C<ev_timer>
watchers, as those cannot react to time jumps.

As with timers, the callback is guaranteed to be invoked only when the
point in time where it is supposed to trigger has passed. If multiple
timers become ready during the same loop iteration then the ones with
earlier time-out values are invoked before ones with later time-out values
(but this is no longer true when a callback calls C<ev_run> recursively).

=head3 Watcher-Specific Functions and Data Members

=over 4

=item ev_periodic_init (ev_periodic *, callback, ev_tstamp offset, ev_tstamp interval, reschedule_cb)

=item ev_periodic_set (ev_periodic *, ev_tstamp offset, ev_tstamp interval, reschedule_cb)

Lots of arguments, let's sort it out... There are basically three modes of
operation, and we will explain them from simplest to most complex:

=over 4

=item * absolute timer (offset = absolute time, interval = 0, reschedule_cb = 0)

In this configuration the watcher triggers an event after the wall clock
time C<offset> has passed. It will not repeat and will not adjust when a
time jump occurs, that is, if it is to be run at January 1st 2011 then it
will be stopped and invoked when the system clock reaches or surpasses
this point in time.

=item * repeating interval timer (offset = offset within interval, interval > 0, reschedule_cb = 0)

In this mode the watcher will always be scheduled to time out at the next
C<offset + N * interval> time (for some integer N, which can also be
negative) and then repeat, regardless of any time jumps. The C<offset>
argument is merely an offset into the C<interval> periods.

This can be used to create timers that do not drift with respect to the
system clock, for example, here is an C<ev_periodic> that triggers each
hour, on the hour (with respect to UTC):

   ev_periodic_set (&periodic, 0., 3600., 0);

This doesn't mean there will always be 3600 seconds in between triggers,
but only that the callback will be called when the system time shows a
full hour (UTC), or more correctly, when the system time is evenly divisible



( run in 0.535 second using v1.01-cache-2.11-cpan-39bf76dae61 )