Using Swift 3 Stopping a scheduledTimer, Timer continue firing even if timer is nil

Try to make the following changes to your code:

First, you have to change the way you declare timerTest

var timerTest : Timer?

then in startTimer before instantiating check if timerTest is nil

func startTimer () {
  guard timerTest == nil else { return }

  timerTest =  Timer.scheduledTimer(
      timeInterval: TimeInterval(0.3),
      target      : self,
      selector    : #selector(ViewController.timerActionTest),
      userInfo    : nil,
      repeats     : true)
}

Finally in your stopTimerTest you invalidate timerTest if it isn’t nil

func stopTimerTest() {
  timerTest?.invalidate()
  timerTest = nil
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)