Contents

init(timeInterval:repeats:block:)

Initializes a timer object with the specified time interval and block.

Declaration

init(timeInterval interval: TimeInterval, repeats: Bool, block: @escaping  @Sendable (Timer) -> Void)

Parameters

  • interval:

    The number of seconds between firings of the timer. If interval is less than or equal to 0.0, this method chooses the nonnegative value of 0.0001 seconds instead.

  • repeats:

    If true, the timer will repeatedly reschedule itself until invalidated. If false, the timer will be invalidated after it fires.

  • block:

    A block to be executed when the timer fires. The block takes a single Timer parameter and has no return value.

Return Value

A new Timer object, configured according to the specified parameters.

Discussion

You must add the new timer to a run loop, using add(_:forMode:). Then, after interval seconds have elapsed, the timer fires, executing block. (If the timer is configured to repeat, you don’t need to add the timer to the run loop again.)

See Also

Creating a Timer