Skip to content

Commit

Permalink
better example for timers outside of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 6, 2014
1 parent 4b316ec commit d8d53d9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -464,14 +464,19 @@ created at startup time.
use Mojolicious::Lite;
use Mojo::IOLoop;

# Count seconds since startup
my $i = 0;
Mojo::IOLoop->recurring(1 => sub { $i++ });
# Check title in the background every 10 seconds
my $title = 'Got no title yet.';
Mojo::IOLoop->recurring(10 => sub {
app->ua->get('http://mojolicio.us' => sub {
my ($ua, $tx) = @_;
$title = $tx->res->dom->at('title')->text;
});
});

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

app->start;
Expand Down

0 comments on commit d8d53d9

Please sign in to comment.