J Kao
J Kao

Reputation: 2113

What is an easy way to do a timer?

I'm writing a Ruby script and want behaviour like this:

every 15 seconds do
  callback
end

How would I do this?

A quick search didn't show many libraries; Should I be using threads? I want this pretty basic since my Ruby script is going to be basic.

Upvotes: 1

Views: 1292

Answers (2)

Ryan LeCompte
Ryan LeCompte

Reputation: 4321

You can also checkout EventMachine's PeriodicTimer class. I've used this with success in the past: http://eventmachine.rubyforge.org/EventMachine/PeriodicTimer.html

EventMachine may be overkill for your needs, though.

Upvotes: 0

Damien Wilson
Damien Wilson

Reputation: 4560

You could always call sleep:

loop do
  sleep 15
  callback
end

Upvotes: 2

Related Questions