0

I want an action to be run after 120 seconds but for the rest of the code to continue to run without interruption is there a way to do this in swift? For example

var timer = 0 if timer == 120{ print("time up") } //But This code still needs to be able to be run if buttonPressed == true{ print("pressed") } 
2
  • 2
    See: stackoverflow.com/a/24318861/1630618 Commented Jun 26, 2016 at 1:01
  • 1
    With the above delay function, you'd do delay(120) { print("time up") } which does that in 2 minutes, but the code that follows executes immediately. Commented Jun 26, 2016 at 1:03

1 Answer 1

0

see https://stackoverflow.com/a/28821805/6496271

let triggerTime = (Int64(NSEC_PER_SEC) * 10) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in self.functionToCall() })

It calls self.functionToCall() after 10 seconds

To make it 120 secs:

let triggerTime = (Int64(NSEC_PER_SEC) * 120) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in self.functionToCall() }) 
Sign up to request clarification or add additional context in comments.

Comments