Questions tagged [nstimer]

An Objective C class for creating timer objects.

In Objective C (), you use the NSTimer class to create timer objects or, more simply, timers. A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. For example, you could create an NSTimer object that sends a message to a window, telling it to update itself after a certain time interval.

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

NSTimer is “toll-free bridged” with its Core Foundation () counterpart, CFRunLoopTimerRef. See “Toll-Free Bridging” for more information on toll-free bridging.

Additional examples for the NSTimer object can be found here.

2624 questions
48
votes
10 answers

Using an NSTimer in Swift

In this scenario, timerFunc() is never called. What am I missing? class AppDelegate: NSObject, NSApplicationDelegate { var myTimer: NSTimer? = nil func timerFunc() { println("timerFunc()") } func…
RobertJoseph
  • 7,968
  • 12
  • 68
  • 113
47
votes
4 answers

Xcode 7.3 / Swift 2: "No method declared with Objective-C selector" warning

I've been using selectors for a while, and even after migrating to Swift I was able to use them without issues. That's how I was using on Swift 2 without issues until I updated Xcode to version 7.3: As use can see I use selectors with NSTimer. This…
tomDev
  • 5,620
  • 5
  • 29
  • 39
44
votes
9 answers

NSTimer with anonymous function / block?

I want to be able to schedule three small events in the future without having to write a function for each. How can I do this using NSTimer? I understand blocks facilitate anonymous functions but can they be used within NSTimer and if so,…
Chris
  • 26,744
  • 48
  • 193
  • 345
41
votes
5 answers

Format timer label to hours:minutes:seconds in Swift

I have an NSTimer which counts DOWN from 2 hours until 0. Here are some of my code: var timer = NSTimer() let timeInterval:NSTimeInterval = 0.5 let timerEnd:NSTimeInterval = 0.0 var timeCount:NSTimeInterval = 7200.0 // seconds or 2 hours //…
Juma Orido
  • 481
  • 1
  • 4
  • 11
40
votes
1 answer

Difference in scheduling NSTimer in main thread and background thread?

When I call scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: on the main thread and set time interval to 5 seconds code below timer gets executed, and after 5 seconds timer selector is called. But if I try same in some background…
MegaManX
  • 8,766
  • 12
  • 51
  • 83
39
votes
1 answer

How Do I write a Timer in Objective-C?

I am trying to make a stop watch with NSTimer. I gave the following code: nst_Timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showTime) userInfo:nil repeats:NO]; and it is not working in milliseconds. It takes…
new_programmer
  • 840
  • 3
  • 12
  • 22
38
votes
3 answers

How to stop NSTimer.scheduledTimerWithTimeInterval

How do i stop my timer from running? Not like a pause, but a stop. import UIKit class LastManStandingViewController: UIViewController { @IBOutlet weak var timeLabel: UILabel! @IBOutlet weak var timeTextbox: UITextField! @IBOutlet weak var…
Erik Auranaune
  • 1,384
  • 1
  • 12
  • 27
37
votes
4 answers

NSTimer not firing the selector

In ios5.0 with ARC, in my rootviewcontroller I call a method in a security manager object that is held by the app delegate. In that method I setup the timer as below NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self …
inforeqd
  • 3,209
  • 6
  • 32
  • 46
34
votes
3 answers

How Can I Start And Stop NSTimer?

I develop Stop Watch Application. In my application, there are Two UIButtons , StartBtn and StopBtn, And also I use NSTimer. Now, i want to start NSTimer when user click on StartBtn and also stop when your click on StopBtn. I know that NSTimer is…
user2289379
33
votes
6 answers

calling a method after each 60 seconds in iPhone

I have created an GKSession and as its object is created, it starts search for availability of devices, as - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { I want to call this method after…
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
33
votes
10 answers

Best time to invalidate NSTimer inside UIViewController to avoid retain cycle

Does any one know when is the best time to stop an NSTimer that is held reference inside of a UIViewController to avoid retain cycle between the timer and the controller? Here is the question in more details: I have an NSTimer inside of a…
32
votes
7 answers

How to stop NSTimer

Hey so i am just making an example application and i am having a little trouble, So i have a table view and then i have a few rows and when a user clicks a row it takes them to a new view. On this view i have a button to play music. I am using a…
cruisx
  • 321
  • 1
  • 3
  • 3
32
votes
9 answers

How can I pause and resume NSTimer.scheduledTimerWithTimeInterval in swift?

I'm developing a game and I want to create a pause menu. Here is my code: self.view?.paused = true but NSTimer.scheduledTimerWithTimeInterval still running... for var i=0; i < rocketCount; i++ { var a: NSTimeInterval = 1 ii += a …
Pandu Arif Septian
  • 340
  • 1
  • 3
  • 5
32
votes
4 answers

NSTimer - how to delay in Swift

I have a problem with delaying computer's move in a game. I've found some solutions but they don't work in my case, e.g. var delay = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: nil, userInfo: nil, repeats: false) I tried to…
Dandy
  • 929
  • 2
  • 14
  • 23
32
votes
4 answers

NSTimer timerWithTimeInterval: not working

I've created a test application with timer before implementing it in my project. It was the first time I'm using timer. But the issue is when I implemented timer using [NSTimer timerWithTimeInterval: target: selector: userInfo: repeats: ]; , it is…
Midhun MP
  • 103,496
  • 31
  • 153
  • 200