JavaScriptKit Documentation

Class JSTimer

public final class JSTimer  

This timer is an abstraction over setInterval / clearInterval and setTimeout / clearTimeout JavaScript functions. It intentionally doesn't match the JavaScript API, as a special care is needed to hold a reference to the timer closure and to call JSClosure.release() on it when the timer is deallocated. As a user, you have to hold a reference to a JSTimer instance for it to stay valid. The JSTimer API is also intentionally trivial, the timer is started right away, and the only way to invalidate the timer is to bring the reference count of the JSTimer instance to zero. For invalidation you should either store the timer in an optional property and assign nil to it, or deallocate the object that owns the timer.

Initializers

init(milliseconds​Delay:​is​Repeating:​callback:​)

public init(millisecondsDelay: Double, isRepeating: Bool = false, callback: @escaping () -> ())  

Creates a new timer instance that calls setInterval or setTimeout JavaScript functions for you under the hood.

Parameters

milliseconds​Delay Double

the amount of milliseconds before the callback closure is executed.

is​Repeating Bool

when true the callback closure is executed repeatedly at given millisecondsDelay intervals indefinitely until the timer is deallocated.

callback @escaping () -> ()

the closure to be executed after a given millisecondsDelay interval.

Properties

is​Repeating

public let isRepeating: Bool

Indicates whether this timer instance calls its callback repeatedly at a given delay.