Skip to content

Commit

Permalink
interval: cleanup code and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Jan 21, 2014
1 parent 4ea7af1 commit 06fb3db
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions opal/browser/interval.rb
@@ -1,6 +1,9 @@
module Browser

# This class wraps `setInterval`.
# Allows you to create an interval that executes the function every given
# seconds.
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/Window.setInterval
class Interval
# @!attribute [r] every
# @return [Number] the seconds every which the block is called
Expand Down Expand Up @@ -32,20 +35,14 @@ def aborted?
end

# Abort the interval, it won't be possible to start it again.
#
# @return [self]
def abort
`#@window.clearInterval(#@id)`

@aborted = true
@id = nil

self
end

# Stop the interval, it will be possible to start it again.
#
# @return [self]
def stop
`#@window.clearInterval(#@id)`

Expand All @@ -54,23 +51,20 @@ def stop
end

# Start the interval if it has been stopped.
#
# @return [self]
def start
raise "the interval has been aborted" if aborted?

return unless stopped?

@id = `#@window.setInterval(#{@block.to_n}, #@every * 1000)`

self
end
end

class Window
# Execute the block every given seconds.
#
# @param time [Float] the seconds between every call
#
# @return [Interval] the object representing the interval
def every(time, &block)
Interval.new(@native, time, &block)
Expand All @@ -80,6 +74,7 @@ def every(time, &block)
end

class Proc
# (see Browser::Window#every)
def every(time)
$window.every(time, &self)
end
Expand Down

0 comments on commit 06fb3db

Please sign in to comment.