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
    See: http://stackoverflow.com/a/24318861/1630618 – vacawama Jun 26 '16 at 01: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. – vacawama Jun 26 '16 at 01:03

1 Answers1

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()
})
Community
  • 1
  • 1
user31415
  • 446
  • 7
  • 16