how to find remaining time on a timer?

Marc Lehmann schmorp at schmorp.de
Thu Nov 28 17:46:13 CET 2013


On Thu, Nov 28, 2013 at 10:30:41AM -0500, Fulko Hew <fulko.hew at gmail.com> wrote:
> How can I find out something about the timer?

You can't - as far as AnyEvent is concerned, the watchers are mostly
opaque data types (they are copyable, but little else).

> Either when it was started, how much time has elapsed,
> or how much time is remaining?

You have to do this manually. For example:

   my $start_time = AE::time; # or AE::now
   my $end_time   = $start_time + 10;
   my $timer = AE::timer $end_time - AE::now, 0, sub { }; # must be AE::now

With these you can find out how much time has elapsed...

   my $elapsed = AE::time - $start_time; # or AE::now

...or how much is remaining (might be negative):

   my remaining = $end_time - AE::time; # or AE::now

This can be optimised a lot, and of course you can create your own watcher
wrapper objetc with access methods and so on.

regarding accessing the unerlying timer object: there is no guarantee that
anyevent timers that use the EV event loop actually are EV timers (they
are in the current implementation, but this is not true for most event
loops, and might change).

If you (ever) want to access the native timers directly, you should just rely
on the event loop itself, e.g. use EV; my $timer = EV::timer ...

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the anyevent mailing list