Deepak Tagadiya
Deepak Tagadiya

Reputation: 2237

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

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?

Upvotes: 0

Views: 2354

Answers (3)

Deepak Tagadiya
Deepak Tagadiya

Reputation: 2237

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()

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27438

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!

Upvotes: 1

vp2698
vp2698

Reputation: 1805

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.

Upvotes: 0

Related Questions