In JavaScript you can do:
setInterval(func,delay);
I can't seem to find anything on google for what I'm actually looking for. Is there a ruby equivalent for this? Thanks in advance.
You can do something like it:
Thread.new do
loop do
sleep delay
# your code here
end
end
Or you can define a function:
# @return [Thread] return loop thread reference
def set_interval(delay)
Thread.new do
loop do
sleep delay
yield # call passed block
end
end
end
When you want to stop the set_interval
, you just call any of these methods: exit
, stop
or kill
.
You can test it into console (irb
or pry
):
t1 = Time.now; t = set_interval(2.5) {puts Time.now - t1}
> 2.500325
> 5.000641
> 7.500924
...
t.kill # stop the set_interval function
I use rufus-scheduler:
scheduler = Rufus::Scheduler.new
scheduler.every '5m' do
# Some Fancy Code Logic That Runs Every 5 Minutes
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With