Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Creating a Timer

In Objective-C, there are three ways to create a timer. The following two class methods automatically register the new timer with the current NSRunLoop object in the default mode (NSDefaultRunLoopMode).

The following two class methods create timers that you may register at a later time by sending the message addTimer:forMode: to an NSRunLoop object.

Finally, you can allocate an NSTimer object yourself and send it an initWithFireDate:interval:target:selector:userInfo:repeats: message. This method allows you to specify an initial fire date independently of the repeat interval. Unlike timers created with the previous methods, this timer instance is not autoreleased, so you are responsible for releasing it when you are done with it. Note that run loops retain their timers, so you can release the timer after you have added it to a run loop.

In Java, you create a timer with the NSTimer constructor, specifying the time interval, target, selector, user info, and whether the timer should repeat. You need to add the timer to the run loop yourself.

Once created, only the timer firing date can be modified, using setFireDate:. All other parameters are immutable after creating the timer.

If you specify that the timer should repeat, it automatically reschedules itself after it fires.



< Previous PageNext Page > Hide TOC


Last updated: 2002-11-12




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice