Skip to content

Commit

Permalink
added another timer example
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 11, 2012
1 parent ad6b929 commit 551bf8b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -306,7 +306,25 @@ or they would just keep getting emitted.

app->start;

And remember that all events are processed cooperatively, so your callbacks
Timers are not tied to a specific request or connection, and can even be
created at startup time.

use Mojolicious::Lite;
use Mojo::IOLoop;

# Count seconds since startup
my $i = 0;
Mojo::IOLoop->recurring(1 => sub { $i++ });

# Show counter
get '/' => sub {
my $self = shift;
$self->render(text => "About $i seconds running!");
};

app->start;

Just remember that all events are processed cooperatively, so your callbacks
shouldn't block for too long.

=head2 WebSocket web service
Expand Down Expand Up @@ -451,7 +469,6 @@ Internally the L<Mojo::IOLoop> reactor can use multiple event loop backends,
L<EV> for example will be automatically used if installed. Which in turn
allows L<AnyEvent> to just work.

#!/usr/bin/env perl
use Mojolicious::Lite;
use EV;
use AnyEvent;
Expand Down

0 comments on commit 551bf8b

Please sign in to comment.