0

How to call function on time? Also How to Call only once time in swift 3?

i need to call one function one time only, so how can possible to call function only one time like after 3 seconds?

Deepak Tagadiya
  • 2,187
  • 15
  • 28
  • Possible duplicate of [How to program a delay in Swift 3](https://stackoverflow.com/questions/38031137/how-to-program-a-delay-in-swift-3) – Dharma Jun 28 '17 at 07:14

3 Answers3

1

You can do something like,

DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
   yourFunction() // call your functin here
}

You can also use DispatchQueue.global().asyncAfter if you don't want to perform your task on main thread!

And refer this post to manage it one time only!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0
private let _onceToken = NSUUID().uuidString

DispatchQueue.once(token: _onceToken) {
     print( "Do This Once!" )
     DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
         yourFunction() // call your functin here
     }
}

It will execute your method only once and delay your method for 3 seconds.

vp2698
  • 1,783
  • 26
  • 28
0

For start timer:

  var timerUpdateArray:Timer!
  func callTimer(){
        self.timerUpdateArray = Timer.scheduledTimer(timeInterval: 1, 
        target: self, selector: #selector(yourFunc), 
        userInfo: nil, repeats: true)
   }

For stop timer:

 self.timerUpdateArray.invalidate()
Deepak Tagadiya
  • 2,187
  • 15
  • 28