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
30
votes
2 answers

IOS: stop a NSTimer

Possible Duplicate: NSTimer doesn't stop I have this code: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) …
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
30
votes
7 answers

Press-and-hold button for "repeat fire"

I have referred to countless other questions about a press-and-hold button but there aren't many related to Swift. I have one function connected to a button using the touchUpInside event: @IBAction func singleFire(sender: AnyObject){ …
Tim
  • 583
  • 1
  • 6
  • 18
30
votes
6 answers

Passing Data through NSTimer UserInfo

I am trying to pass data through userInfo for an NSTimer call. What is the best way to do this? I am trying to use an NSDictionary, this is simple enough when I have Objective-C objects, but what about other data? I want to do something like this,…
zorro2b
  • 2,227
  • 4
  • 28
  • 45
30
votes
4 answers

How to find if NSTimer is active or not?

I have a timer something like this: NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) …
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
29
votes
5 answers

swift invalidate timer doesn't work

I have this problem for a few days now and I don't get what I am doing wrong. My application is basically just creating some timers. I need to stop them and create new ones. But at the moment stopping them doesn't work. self.timer =…
Banelu
  • 293
  • 1
  • 3
  • 9
27
votes
1 answer

CountDown Timer ios tutorial?

I'm making an application where the exams is going on, so when exam start the, time should start with that. For example 30 minutes and it should reduce like 29:59. How can I implement this? Can anyone please give me a sample example or a easy step…
zeeshan shaikh
  • 819
  • 3
  • 18
  • 33
24
votes
2 answers

NSTimer not firing when runloop is blocked

I am just about finished with my app and beta testing found a bug in the stopwatch portion... The stopwatch uses an nstimer to do the counting and has a table for storing laps, but when the lap table is scrolled the watch stops or pauses and does…
echobravo
  • 393
  • 1
  • 3
  • 11
24
votes
2 answers

NSTimer vs CACurrentMediaTime()

So I'm amidst my first iOS game and am struggling with how to go about the best way to integrate object movement. The game relies heavily on fast moving objects and constant, fast user input changes. As such, I'm trying to have object integration…
Z.O.
  • 395
  • 1
  • 4
  • 12
23
votes
2 answers

How to "validate" a NSTimer after invalidating it?

So basically, I have this timer that should be repeated when it receives a key event, and invalidates when the user releases the key. However, I am unable to "validate" the timer back even by calling the addTimer:forMode: in NSRunLoop. Does anyone…
TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
23
votes
4 answers

How to call a method every x seconds in Objective-C using NSTimer?

I am using Objective-C, Xcode 4.5.1 and working on an app for the iPhone. I have a method A in which I want to call another method B to do a series of calculations every x seconds. In method A I start playing an audio file. Method B will monitor the…
RoelfMik
  • 255
  • 1
  • 2
  • 9
22
votes
6 answers

NSTimer requiring me to add it to a runloop

I am wondering if someone can explain why dispatching back to the main queue and creating a repeating NSTimer I am having to add it to RUN LOOP for it too fire? Even when using performselectorOnMainThread I still have to add it to a RUN LOOP to get…
chicken
  • 1,618
  • 5
  • 24
  • 35
22
votes
3 answers

Run repeating NSTimer with GCD?

I was wondering why when you create a repeating timer in a GCD block it doesen't work? This works fine: -(void)viewDidLoad{ [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer)…
Praxder
  • 2,315
  • 4
  • 32
  • 51
21
votes
2 answers

Passing parameters to a method called by NSTimer in Swift

I'm trying to pass an argument to a method that is called by NSTimer in my code. It is throwing an exception. This is how I'm doing it. Circle is my custom class. var circle = Circle() var timer = NSTimer.scheduledTimerWithInterval(1.0,…
Raghu
  • 361
  • 1
  • 2
  • 11
21
votes
6 answers

Implementing a Countdown Timer in Objective-c?

I am new to iOS programming. I am working on words matching game. In this game I need to implement time counter which shows minutes and seconds. I want when my game is started my timer to start with 3 minutes. Now I want to decrease this timer in…
jamil
  • 2,419
  • 3
  • 37
  • 64
19
votes
5 answers

How do I change timing for NSTimer?

I have the following code: timer = [[NSTimer scheduledTimerWithTimeInterval:0.50 target:self selector:@selector(onTimer) userInfo:nil repeats:YES] retain]; -(void) onTimer { } After every 0.50 seconds the OnTimer method is called. But now I…
Hardik Patel
  • 937
  • 3
  • 14
  • 39