Skip to content

AsyncTimer

Oleksandr Bretsko edited this page Dec 27, 2020 · 1 revision

AsyncTimer

The timer can be used to execute periodically provided block on any queue (main, private). It allows scheduling resume, suspend and restart operations in future (at specific date, or after given elapsed time) It also supports loop mode, in which it will restart after a given interval It can be in 2 states - running and suspended

public class AsyncTimer: AsyncTimerP

Inheritance

AsyncTimerP

Initializers

init(interval:startDelay:_:_:)

Inits timer ready to resume with given interval, startDelay and handler

public init(interval: Double, startDelay: Double? = nil, _ queue: DispatchQueue = .main, _ handler: @escaping AsyncTimerBlock)

Parameters

  • interval: frequency of executing the handler, in seconds (rounded to milliseconds)
  • startDelay: delay interval of start, in seconds (rounded to milliseconds)

Properties

state

var state: TimerState = .suspended

interval

Frequency of executing the handler Interval is in seconds (rounded to milliseconds)

var interval: Double

startDelay

Delay interval for each resume operation Interval is in seconds (rounded to milliseconds)

var startDelay: Double

elapsedTime

Time elapsed since timer was (re)started Interval is in seconds (rounded to milliseconds)

var elapsedTime: Double = 0.0

observer

var observer: AsyncTimerObserverP?

autoStopOnElapsedTime

When set - suspends timer when elapsed time reaches this value (and this setting is the reset to nil) Time is in seconds (rounded to milliseconds)

var autoStopOnElapsedTime: Double?

loopInterval

When set timer works in loop mode, by restarting when the elapsedTime reaches the value of loopInterval Interval is in seconds (rounded to milliseconds)

var loopInterval: Double?

autoStopOnDate

When set - suspends timer when this date is reached (and this setting is the reset to nil) Interval is in seconds (rounded to milliseconds)

var autoStopOnDate: Date?

Methods

setLoopInterval(_:)

Sets timer to work in loop mode, by restarting when the elapsedTime reaches the value of loopInterval

public func setLoopInterval(_ interval: Double)

Parameters

  • elapsedTime: time interval in seconds (rounded to milliseconds)

resume(interval:after:_:)

Resumes currently suspended timer with given paramaters

@discardableResult public func resume(interval: Double? = nil, after startDelay: Double? = nil, _ handler: (AsyncTimerBlock)? = nil) -> Bool

Parameters

  • interval: frequency of executing the handler, in seconds (rounded to milliseconds)
  • startDelay: startDelay, in seconds (rounded to milliseconds)

Returns

true if succeeded

resume(after:)

Resumes timer, if it's suspended

@discardableResult public func resume(after delayInterval: Double) -> Bool

Parameters

  • delayInterval: delayInterval, in seconds (rounded to milliseconds)

Returns

true if succeeded

resume(on:)

Schedules resuming timer when this date is reached, if it's suspended

@discardableResult public func resume(on date: Date) -> Bool

Returns

true if succeeded

resume(on:)

Schedules resuming timer when elapsed time reaches this value, if it's suspended

@discardableResult public func resume(on elapsedTime: Double) -> Bool

Parameters

  • elapsedTime: elapsedTime, in seconds (rounded to milliseconds)

Returns

true if succeeded

suspend()

Suspends timer if it's running

@discardableResult public func suspend() -> Bool

Returns

true if succeeded

suspend(on:)

Schedules suspending timer when this date is reached

@discardableResult public func suspend(on date: Date) -> Bool

Parameters

  • interval: frequency of executing the handler, in seconds (rounded to milliseconds)

Returns

true if succeeded

suspend(after:)

Suspends timer if it's running, after given interval

@discardableResult public func suspend(after delayInterval: Double) -> Bool

Parameters

  • delayInterval: delayInterval, in seconds (rounded to milliseconds)

Returns

true if succeeded

suspend(on:)

Schedules suspending timer when elapsed time reaches this value, if it's running

@discardableResult public func suspend(on elapsedTime: Double) -> Bool

Parameters

  • elapsedTime: elapsed time interval in seconds (rounded to milliseconds)

Returns

true if succeeded

restart(interval:startDelay:_:)

Suspends currently running timer and starts again If interval or handler not given - reuses the last used ones

@discardableResult public func restart(interval: Double? = nil, startDelay: Double? = nil, _ handler: (AsyncTimerBlock)? = nil) -> Bool

Parameters

  • interval: frequency of executing the handler, in seconds (rounded to milliseconds)
  • startDelay: startDelay, in seconds (rounded to milliseconds)

Returns

true if succeeded

restart(on:)

Schedules restarting timer when this date is reached

public func restart(on date: Date) -> Bool

Returns

true if succeeded

restart(on:)

Schedules restarting timer when elapsed time reaches this value

public func restart(on elapsedTime: Double) -> Bool

Parameters

  • elapsedTime: elapsedTime, in seconds (rounded to milliseconds)

Returns

true if succeeded

restart(after:)

Schedules restarting timer after given interval (in seconds)

public func restart(after interval: Double) -> Bool

Parameters

  • interval: delay interval, in seconds (rounded to milliseconds)

Returns

true if succeeded

loop(interval:)

Starts running timer in loop mode with given interval Timer must be in suspended state to run in loop mode

@discardableResult public func loop(interval: Double) -> Bool

Returns

true if succeeded

setHandler(_:)

Sets handler for this timer on main queue

public func setHandler(_ handler: @escaping AsyncTimerBlock)

setHandler(_:_:)

Sets handler for this timer on given queue

public func setHandler(_ handler: @escaping AsyncTimerBlock, _ queue: DispatchQueue)

setTimeInterval(_:startDelay:)

If startDelay is not nil will delay start by given number of seconds.

@discardableResult public func setTimeInterval(_ interval: Double, startDelay: Double? = nil) -> Bool

Parameters

  • interval: frequency of executing the handler, in seconds (rounded to milliseconds)
  • startDelay: startDelay, in seconds (rounded to milliseconds)

Returns

true if succeeded

delayStart(by:)

Sets startDelay interval, which will delay each resume operation by a given number of seconds

@discardableResult public func delayStart(by delayInterval: Double) -> Bool

Parameters

  • delayInterval: time interval for delay of start in seconds (rounded to milliseconds)

Returns

true if succeeded

resetSettings()

Suspends timer if it's running, and resets settings (loop, autoStop, restart)

public func resetSettings()