Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a Ruby on Rails application where data from an online database is pulled and stored in my local database. However, this process only happens every time I re-load the page.

Moreover, I would like to create something (maybe a timeout), that would allow me to run the code every amount of time (2 minutes for example). I do not know how to do that.

Thank you.

What I have tried:

I do not know where to start, thus I have tried anything yet.
Posted
Updated 17-Apr-18 4:12am
v2

1 solution

I used this:

def show
  every_n_seconds (120) do
     #code to do
 end
end


And all the way at the top, (so that ruby sees that method before anything else), I have:

def every_n_seconds(n)
    loop do
      before = Time.now
      yield
      interval = n-(Time.now-before)
      sleep(interval) if interval > 0
    end
  end


It is working quite nice. Thanks everyone anyway. I hope this can help someone in the future.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900