Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created January 29, 2012 06:00
Show Gist options
  • Select an option

  • Save practicingruby/1697485 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/1697485 to your computer and use it in GitHub Desktop.
class Throttler
def initialize(delay)
self.delay = delay
self.timestamp = Time.now
end
def execute(&callback)
elapsed = Time.now - timestamp
sleep(delay - elapsed) if elapsed < delay
callback.call
self.timestamp = Time.now
end
private
attr_accessor :delay, :timestamp
end
t = Throttler.new(0.2)
t.execute { puts "A" }
t.execute { puts "B" }
t.execute { puts "C" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment