Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Although not the original question by the OP, certain NSTimer related questions have been marked as duplicates of this question, so it is worth including an NSTimer answer here.

#NSTimer vs dispatch_after

  • NSTimer is more high level while dispatch_after is more low level.
  • NSTimer is easier to cancel. Canceling dispatch_after requires writing more codemore code.

#Delaying a task with NSTimer

Create an NSTimer instance.

var timer = NSTimer() 

Start the timer with the delay that you need.

// invalidate the timer if there is any chance that it could have been called before timer.invalidate() // delay of 2 seconds timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 

Add a function to be called after the delay (using whatever name you used for the selector parameter above).

func delayedAction() { print("Delayed action has now started." } 

#Notes

  • If you need to cancel the action before it happens, simply call timer.invalidate().

  • For a repeated action use repeats: true.

  • If you have a one time event with no need to cancel then there is no need to create the timer instance variable. The following will suffice:

     NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 
  • See my fuller answer herehere.

Although not the original question by the OP, certain NSTimer related questions have been marked as duplicates of this question, so it is worth including an NSTimer answer here.

#NSTimer vs dispatch_after

  • NSTimer is more high level while dispatch_after is more low level.
  • NSTimer is easier to cancel. Canceling dispatch_after requires writing more code.

#Delaying a task with NSTimer

Create an NSTimer instance.

var timer = NSTimer() 

Start the timer with the delay that you need.

// invalidate the timer if there is any chance that it could have been called before timer.invalidate() // delay of 2 seconds timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 

Add a function to be called after the delay (using whatever name you used for the selector parameter above).

func delayedAction() { print("Delayed action has now started." } 

#Notes

  • If you need to cancel the action before it happens, simply call timer.invalidate().

  • For a repeated action use repeats: true.

  • If you have a one time event with no need to cancel then there is no need to create the timer instance variable. The following will suffice:

     NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 
  • See my fuller answer here.

Although not the original question by the OP, certain NSTimer related questions have been marked as duplicates of this question, so it is worth including an NSTimer answer here.

#NSTimer vs dispatch_after

  • NSTimer is more high level while dispatch_after is more low level.
  • NSTimer is easier to cancel. Canceling dispatch_after requires writing more code.

#Delaying a task with NSTimer

Create an NSTimer instance.

var timer = NSTimer() 

Start the timer with the delay that you need.

// invalidate the timer if there is any chance that it could have been called before timer.invalidate() // delay of 2 seconds timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 

Add a function to be called after the delay (using whatever name you used for the selector parameter above).

func delayedAction() { print("Delayed action has now started." } 

#Notes

  • If you need to cancel the action before it happens, simply call timer.invalidate().

  • For a repeated action use repeats: true.

  • If you have a one time event with no need to cancel then there is no need to create the timer instance variable. The following will suffice:

     NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 
  • See my fuller answer here.

Source Link
Suragch
  • 516.3k
  • 340
  • 1.4k
  • 1.4k

Although not the original question by the OP, certain NSTimer related questions have been marked as duplicates of this question, so it is worth including an NSTimer answer here.

#NSTimer vs dispatch_after

  • NSTimer is more high level while dispatch_after is more low level.
  • NSTimer is easier to cancel. Canceling dispatch_after requires writing more code.

#Delaying a task with NSTimer

Create an NSTimer instance.

var timer = NSTimer() 

Start the timer with the delay that you need.

// invalidate the timer if there is any chance that it could have been called before timer.invalidate() // delay of 2 seconds timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 

Add a function to be called after the delay (using whatever name you used for the selector parameter above).

func delayedAction() { print("Delayed action has now started." } 

#Notes

  • If you need to cancel the action before it happens, simply call timer.invalidate().

  • For a repeated action use repeats: true.

  • If you have a one time event with no need to cancel then there is no need to create the timer instance variable. The following will suffice:

     NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 
  • See my fuller answer here.