Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Call function no more than every 0.1 seconds

by Fletch (Bishop)
on Aug 12, 2020 at 04:16 UTC ( [id://11120640]=note: print w/replies, xml ) Need Help??


in reply to Call function no more than every 0.1 seconds

Possibly overkill for this situation but in a more general case an event loop like POE (or AnyEvent or EV or Mojo::IOLoop or . . .) might be useful (especially if you're going to need to handle other timing, or react to outside IO events (network traffic, IO from files, . . .). You'd call your routine in an event handler, then after it returns request to be delivered the same event to yourself after the requisite delay (starting things off immediately sending yourself the event).

Edit: Mojo version.

#!/usr/bin/env perl use 5.032; use Mojo; use Mojo::IOLoop; use Mojo::Log; my $log = Mojo::Log->new; my $interval = 0.25; my $ctr = 1; sub callback { my $loop = shift; $log->debug( qq{: Timer, counter } . $ctr++ ); $loop->timer( $interval => \&callback ) if $ctr <= 5; } Mojo::IOLoop->timer( $interval => \&callback ); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; exit 0; __END__

The cake is a lie.
The cake is a lie.
The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120640]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-16 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found