Timer

extension Timer

Schedule timers

  • Create and schedule a timer that will call block once after the specified time.

    Declaration

    Swift

    @discardableResult
    public class func after(_ interval: TimeInterval, _ block: @escaping () -> Void) -> Timer
  • Create and schedule a timer that will call block repeatedly in specified time intervals.

    Declaration

    Swift

    @discardableResult
    public class func every(_ interval: TimeInterval, _ block: @escaping () -> Void) -> Timer
  • Create and schedule a timer that will call block repeatedly in specified time intervals. (This variant also passes the timer instance to the block)

    Declaration

    Swift

    @discardableResult
    public class func every(_ interval: TimeInterval, _ block: @escaping (Timer) -> Void) -> Timer

Create timers without scheduling

  • Create a timer that will call block once after the specified time.

    Note

    The timer won’t fire until it’s scheduled on the run loop. Use NSTimer.after to create and schedule a timer in one step.

    Note

    The new class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)

    Declaration

    Swift

    public class func new(after interval: TimeInterval, _ block: @escaping () -> Void) -> Timer
  • Create a timer that will call block repeatedly in specified time intervals.

    Note

    The timer won’t fire until it’s scheduled on the run loop. Use NSTimer.every to create and schedule a timer in one step.

    Note

    The new class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)

    Declaration

    Swift

    public class func new(every interval: TimeInterval, _ block: @escaping () -> Void) -> Timer
  • Create a timer that will call block repeatedly in specified time intervals. (This variant also passes the timer instance to the block)

    Note

    The timer won’t fire until it’s scheduled on the run loop. Use NSTimer.every to create and schedule a timer in one step.

    Note

    The new class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)

    Declaration

    Swift

    public class func new(every interval: TimeInterval, _ block: @escaping (Timer) -> Void) -> Timer

Manual scheduling

  • Schedule this timer on the run loop

    By default, the timer is scheduled on the current run loop for the default mode. Specify runLoop or modes to override these defaults.

    Declaration

    Swift

    public func start(runLoop: RunLoop = .current, modes: RunLoop.Mode...)