AnyEvent::Debounce - condense multiple temporally-nearby events into one


version 0.01


Create a debouncer:

   my $damper = AnyEvent::Debounce->new( cb => sub {
       my (@events) = @_;
       say "Got ", scalar @events, " event(s) in the batch";
       say "Got event with args: ", join ',', @$_ for @events;

Send it events in rapid succession:


Watch the output:

   Got 2 events in the batch
   Got event with args: 1,2,3
   Got event with args: 2,3,4

Send it more evnts:

   sleep 5;

And notice that there was no need to "debounce" this time:

   Got 1 event in the batch
   Got event with args: 1

   Got 1 event in the batch
   Got event with args: 2



The callback to be called when some events are ready to be handled. Each "event" is an arrayref of the args passed to send.


The time to wait after receiving an event before sending it, in case more events happen in the interim.


Normally, when an event is received and it's the first of a series, a timer is started, and when that timer expires, all events are sent. If you set this initarg to a true value, then the timer is reset after each event is received.

For example, if you set the delay to 1, and ten events arrive at 0.5 second intervals, then with this flag set to true, you will get one event after 5 seconds. With this flag set to false, you will get an event once a second for 5 seconds.

By default, this is false, because setting it to true can lead to events never being sent. (Imagine you set delay to 10 seconds, and someone sends an event ever 9.9 seconds. You'll never get any events.)


This flag, when set to true, causes an event to be sent immediately upon receiving the first event. Then, you won't get any events for delay seconds, even if they occur. These events are lost, you will never see them.

By default, this is false.

If you also set always_reset_timer to true, the same timer-reset logic as described above occurs.



Send an event; the handler callback will get everything you pass in.



Jonathan Rockway <>


This software is copyright (c) 2011 by Jonathan Rockway.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.