Sometimes you could have the need to execute the same functions at regular intervals of time. To do this job you can instantiate a new thread (posix or NSThread as shown in "Threading on iOS – How to create a new working thread.")

Then with a while(1) loop you can repeat the functions you want.


There is another way to do this job, you can use the NSTimer Class:


From the Apple Documentation:

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.


Imagine you want to print a counter each seconds, using the NSTimer you can do the following:


– (void)viewDidLoad


    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(counterPrinter) userInfo:nil repeats:YES];


– (void) counterPrinter


    static int ii=0;


    NSLog(@"%d", ii);




To stop a timer you have to invalidate it. So you must have a reference to the timer.

    [myTimer invalisate];

    myTimer = nil;