Search code examples
swifttimerviewdidload

Make Timer start counting as soon as ViewDidLoad is called


I have set Timer in ViewDidLoad in this way:

let timer = Timer.scheduledTimer(
           timeInterval: 3, target: self, selector: #selector(handleAnimation),
       userInfo: nil, repeats: true)

But until it fires out the first handleAnimation method I need to wait for 3 seconds. Is there a way to make timer fire out the function as soon as the viewDidLoad function is executed?


Solution

  • Tell the timer fo fire immediately.

    let timer = Timer.scheduledTimer(
           timeInterval: 3, target: self, selector: #selector(handleAnimation),
       userInfo: nil, repeats: true)
    timer.fire()