Timer

public final class Timer: NSObject

You use the Timer class to create timer objects or, more simply, timers. A timer waits until a certain time interval has elapsed and then fires, executing a specified block of code.

  • The current number of times the timer has fired.

    Declaration

    Swift

    public internal(set) var step = 0
  • The number of times the timer will fire.

    Declaration

    Swift

    public internal(set) var count: Int
  • The time interval between firing.

    Declaration

    Swift

    public internal(set) var interval: Double
  • Initializes a new timer.

    let t = Timer(0.25) {
        print("tick")
    }
    

    Declaration

    Swift

    public init(interval: Double, count: Int = Int.max, action: () -> ())

    Parameters

    interval

    the time between firing

    count

    the total number of times the timer should fire, defaults to Int.max

    action

    a block of code to execute

  • Tells the timer to fire, i.e. execute its block of code.

    Declaration

    Swift

    public func fire()
  • Tells the timer to attach itself to the main run loop of an application, after calling start the timer will continue firing until the timer reaches its count or is otherwise stopped.

    Declaration

    Swift

    public func start()
  • Pauses the execution of the timer.

    Declaration

    Swift

    public func pause()
  • Stops the timer and resets its step to 0.

    Declaration

    Swift

    public func stop()