<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Objective-C_-_NSTimer_Usage</id>
	<title>Objective-C - NSTimer Usage - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Objective-C_-_NSTimer_Usage"/>
	<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Objective-C_-_NSTimer_Usage&amp;action=history"/>
	<updated>2026-05-18T18:30:53Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://performiq.com/kb/index.php?title=Objective-C_-_NSTimer_Usage&amp;diff=3999&amp;oldid=prev</id>
		<title>PeterHarding: Created page with &quot; =References=   * http://developer.apple.com/library/ios/#Documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html * https://developer.apple.com/library/mac/#documentat...&quot;</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Objective-C_-_NSTimer_Usage&amp;diff=3999&amp;oldid=prev"/>
		<updated>2013-04-17T06:16:00Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; =References=   * http://developer.apple.com/library/ios/#Documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html * https://developer.apple.com/library/mac/#documentat...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* http://developer.apple.com/library/ios/#Documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html&lt;br /&gt;
* https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Timers/Timers.html&lt;br /&gt;
* https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html&lt;br /&gt;
&lt;br /&gt;
* http://blog.datispars.com/cocoa-nstimer-example/&lt;br /&gt;
&lt;br /&gt;
* http://www.icodeblog.com/2009/07/23/nstimer-the-poor-mans-threading-code-snapshot/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 performSelector:withObject:afterDelay:&lt;br /&gt;
&lt;br /&gt;
Invokes a method of the receiver on the current thread using the default mode after a delay.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Parameters&lt;br /&gt;
aSelector&lt;br /&gt;
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.&lt;br /&gt;
anArgument&lt;br /&gt;
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.&lt;br /&gt;
delay&lt;br /&gt;
The minimum time before which the message is sent. Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.&lt;br /&gt;
Discussion&lt;br /&gt;
This method sets up a timer to perform the aSelector message on the current thread’s run loop. The timer is configured to run in the default mode (NSDefaultRunLoopMode). When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector. It succeeds if the run loop is running and in the default mode; otherwise, the timer waits until the run loop is in the default mode.&lt;br /&gt;
&lt;br /&gt;
If you want the message to be dequeued when the run loop is in a mode other than the default mode, use the performSelector:withObject:afterDelay:inModes: method instead. If you are not sure whether the current thread is the main thread, you can use the performSelectorOnMainThread:withObject:waitUntilDone: or performSelectorOnMainThread:withObject:waitUntilDone:modes: method to guarantee that your selector executes on the main thread. To cancel a queued message, use the cancelPreviousPerformRequestsWithTarget: or cancelPreviousPerformRequestsWithTarget:selector:object: method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
- (NSTimer*)createTimer {&lt;br /&gt;
&lt;br /&gt;
    // create timer on run loop&lt;br /&gt;
    return [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES];&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
- (void)timerTicked:(NSTimer*)timer {&lt;br /&gt;
&lt;br /&gt;
    // decrement timer 1 … this is your UI, tick down and redraw&lt;br /&gt;
    [myStopwatch tickDown];&lt;br /&gt;
    [myStopwatch.view setNeedsDisplay]; &lt;br /&gt;
&lt;br /&gt;
    // increment timer 2 … bump time and redraw in UI&lt;br /&gt;
    …&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
- (void)actionStop:(id)sender {&lt;br /&gt;
&lt;br /&gt;
    // stop the timer&lt;br /&gt;
    [myTimer invalidate];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* http://stackoverflow.com/questions/1449035/how-do-i-use-nstimer&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[NSTimer scheduledTimerWithTimeInterval:2.0&lt;br /&gt;
    target:self&lt;br /&gt;
    selector:@selector(targetMethod:)&lt;br /&gt;
    userInfo:nil&lt;br /&gt;
    repeats:NO];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[myTimer invalidate];&lt;br /&gt;
myTimer = nil;\&lt;br /&gt;
&lt;br /&gt;
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0&lt;br /&gt;
                      target: self&lt;br /&gt;
                      selector:@selector(onTick:)&lt;br /&gt;
                      userInfo: nil repeats:NO];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[self performSelector:@selector(onTick:) withObject:nil afterDelay:2.0];&lt;br /&gt;
&lt;br /&gt;
NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 60.0];&lt;br /&gt;
NSTimer *t = [[NSTimer alloc] initWithFireDate: d&lt;br /&gt;
                              interval: 1&lt;br /&gt;
                              target: self&lt;br /&gt;
                              selector:@selector(onTick:)&lt;br /&gt;
                              userInfo:nil repeats:YES];&lt;br /&gt;
&lt;br /&gt;
NSRunLoop *runner = [NSRunLoop currentRunLoop];&lt;br /&gt;
[runner addTimer:t forMode: NSDefaultRunLoopMode];&lt;br /&gt;
[t release];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(onTick:)];&lt;br /&gt;
NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn];&lt;br /&gt;
[inv setTarget: self];&lt;br /&gt;
[inv setSelector:@selector(onTick:)];&lt;br /&gt;
&lt;br /&gt;
NSTimer *t = [NSTimer timerWithTimeInterval: 1.0&lt;br /&gt;
                      invocation:inv &lt;br /&gt;
                      repeats:YES];&lt;br /&gt;
and after that, you start the timer manually whenever you need like this:&lt;br /&gt;
&lt;br /&gt;
NSRunLoop *runner = [NSRunLoop currentRunLoop];&lt;br /&gt;
[runner addTimer: t forMode: NSDefaultRunLoopMode];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And as a note, onTick: method looks like this:&lt;br /&gt;
&lt;br /&gt;
-(void)onTick:(NSTimer *)timer {&lt;br /&gt;
   //do smth&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
NSTimer *timer;&lt;br /&gt;
&lt;br /&gt;
    timer = [NSTimer scheduledTimerWithTimeInterval: 0.5&lt;br /&gt;
                     target: self&lt;br /&gt;
                     selector: @selector(handleTimer:)&lt;br /&gt;
                     userInfo: nil&lt;br /&gt;
                     repeats: YES];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
timerNoTwo = [NSTimer scheduledTimerWithTimeInterval:5&lt;br /&gt;
   target:self&lt;br /&gt;
   selector:@selector(updateTimerNoTwo:)&lt;br /&gt;
   userInfo:nil&lt;br /&gt;
   repeats:YES];&lt;br /&gt;
&lt;br /&gt;
- (void) updateTimerNoTwo:(NSTimer *) timer {&lt;br /&gt;
	[timerTwoOutputLabel setIntValue:&lt;br /&gt;
            [timerTwoOutputLabel intValue]+1];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//I am creating a dictionary object that I can pass to my selector method.&lt;br /&gt;
NSArray *keys = [NSArray arrayWithObjects:@&amp;quot;key1&amp;quot;, @&amp;quot;key2&amp;quot;, @&amp;quot;key3&amp;quot;, nil];&lt;br /&gt;
NSArray *objects = [NSArray arrayWithObjects:@&amp;quot;value1&amp;quot;, @&amp;quot;value2&amp;quot;, @&amp;quot;value3&amp;quot;, nil];&lt;br /&gt;
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];&lt;br /&gt;
&lt;br /&gt;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fireTimer:) userInfo:myDictionary repeats:YES];&lt;br /&gt;
&lt;br /&gt;
//The fireTimer() is the selector method I setup above&lt;br /&gt;
- (void) fireTimer:(NSTimer*)theTimer {&lt;br /&gt;
     for (id key in [theTimer userInfo]) {&lt;br /&gt;
&lt;br /&gt;
          NSLog(@&amp;quot;key: %@, value: %@&amp;quot;, key, [[theTimer userInfo] objectForKey:key]);&lt;br /&gt;
     }&lt;br /&gt;
&lt;br /&gt;
//kill the timer&lt;br /&gt;
[theTimer invalidate];&lt;br /&gt;
theTimer = nil;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/************************************************/&lt;br /&gt;
&lt;br /&gt;
//here is a similar example but with using an NSNumber&lt;br /&gt;
NSNumber *myInt = [NSNumber numberWithInt:4];&lt;br /&gt;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fireTimer:) userInfo:myInt repeats:YES];	&lt;br /&gt;
&lt;br /&gt;
- (void) fireTimer:(NSTimer*)theTimer {&lt;br /&gt;
	NSLog(@&amp;quot; %i &amp;quot;, [[theTimer userInfo] integerValue]);&lt;br /&gt;
&lt;br /&gt;
	[theTimer invalidate];&lt;br /&gt;
	theTimer = nil;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@interface className&lt;br /&gt;
{&lt;br /&gt;
NSTimer * timer;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@property (nonatomic, retain) NSTimer * timer;&lt;br /&gt;
&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@implementation className&lt;br /&gt;
@synthesize timer;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-(void) applicationDidFinishLaunching : (UIApplication *) application {&lt;br /&gt;
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//define the targetmethod&lt;br /&gt;
-(void) targetMethod: NSTimer * theTimer {&lt;br /&gt;
NSLog(@\&amp;quot;Me is here at 1 minute delay\&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
..&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
NSTimer *mainTimer = [NSTimer scheduledTimerWithTimeInterval:1 &lt;br /&gt;
                  target:self &lt;br /&gt;
                  selector:@selector(timerController) &lt;br /&gt;
                  userInfo:nil &lt;br /&gt;
                  repeats:YES];&lt;br /&gt;
&lt;br /&gt;
- (void)timerController {&lt;br /&gt;
  seconds++;&lt;br /&gt;
  [[self timeLabel] setText:[self getTimeStr]];&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
- (NSString*)getTimeStr : (int) secondsElapsed {&lt;br /&gt;
  int seconds = secondsElapsed % 60;&lt;br /&gt;
  int minutes = secondsElapsed / 60;&lt;br /&gt;
  return [NSString stringWithFormat:@&amp;quot;%02d:%02d&amp;quot;, minutes, seconds];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Games=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://maniacdev.com/2010/01/incredible-iphone-game-programming-tutorials-with-video/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Objective-C]]&lt;/div&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
</feed>