Class: Browser::Interval

Inherits:
Object show all
Defined in:
opal/browser/interval.rb

Overview

Allows you to create an interval that executes the function every given seconds.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, time, &block) ⇒ Interval

Create and start an interval.

Parameters:

  • window (Window)

    the window to start the interval on

  • time (Float)

    seconds every which to call the block


16
17
18
19
20
21
22
# File 'opal/browser/interval.rb', line 16

def initialize(window, time, &block)
  @window = Native.convert(window)
  @every  = time
  @block  = block

  @aborted = false
end

Instance Attribute Details

#everyFloat (readonly)

Returns the seconds every which the block is called.

Returns:

  • (Float)

    the seconds every which the block is called


10
11
12
# File 'opal/browser/interval.rb', line 10

def every
  @every
end

Instance Method Details

#abortObject

Abort the interval, it won't be possible to start it again.


35
36
37
38
39
40
# File 'opal/browser/interval.rb', line 35

def abort
  `#@window.clearInterval(#@id)`

  @aborted = true
  @id      = nil
end

#aborted?Boolean

Check if the interval has been aborted.

Returns:

  • (Boolean)

30
31
32
# File 'opal/browser/interval.rb', line 30

def aborted?
  @aborted
end

#callObject

Call the [Interval] block.


61
62
63
# File 'opal/browser/interval.rb', line 61

def call
  @block.call
end

#startObject

Start the interval if it has been stopped.


53
54
55
56
57
58
# File 'opal/browser/interval.rb', line 53

def start
  raise "the interval has been aborted" if aborted?
  return unless stopped?

  @id = `#@window.setInterval(#@block, #@every * 1000)`
end

#stopObject

Stop the interval, it will be possible to start it again.


43
44
45
46
47
48
49
50
# File 'opal/browser/interval.rb', line 43

def stop
  return if stopped?

  `#@window.clearInterval(#@id)`

  @stopped = true
  @id      = nil
end

#stopped?Boolean

Check if the interval has been stopped.

Returns:

  • (Boolean)

25
26
27
# File 'opal/browser/interval.rb', line 25

def stopped?
  @id.nil?
end