- Inherits from:
- NSObject
- Conforms to:
- NSObject
- (NSObject)
Declared in:
- Foundation/NSTimer.h
NSTimer creates 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 specified object. For example, you could create an NSTimer that sends a message to a window, telling it to update itself after a certain time interval.
Timers work in conjunction with NSRunLoop objects. NSRunLoops control loops that wait for input, and they use timers to help determine the maximum amount of time they should wait. When the timer's time limit has elapsed, the NSRunLoop fires the timer (causing its message to be sent), then checks for new input. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for an NSTimer is limited to on the order of 50-100 milliseconds.
There are two ways to create a timer. The scheduledTimerWithTimeInterval:invocation:repeats: and scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class
methods automatically register the new timer with the current NSRunLoop
object in the default mode (NSDefaultRunLoopMode
).
The timerWithTimeInterval:invocation:repeats: and timerWithTimeInterval:target:selector:userInfo:repeats: class
methods create timers that you may register at a later time by sending
the message addTimer:forMode: to
the NSRunLoop. If you specify that the timer should repeat, it will
automatically reschedule itself after it fires.
There is no method that removes the association of a timer from an NSRunLoop-send the timer the invalidate message instead. invalidate disables the timer, so it will no longer affect the NSRunLoop.
See the NSRunLoop class description for more information on NSRunLoops.
- Creating a timer
- + scheduledTimerWithTimeInterval:invocation:repeats:
- + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
- + timerWithTimeInterval:invocation:repeats:
- + timerWithTimeInterval:target:selector:userInfo:repeats:
- Firing a timer
- - fire
- Stopping a timer
- - invalidate
- Getting information about a timer
- - isValid
- - fireDate
- - timeInterval
- - userInfo
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
invocation:(NSInvocation *)invocation
repeats:(BOOL)repeats
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats
If seconds is less than or equal to 0.0, this method chooses a nonnegative interval. If repeats is YES, the timer will repeatedly reschedule itself.
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds
invocation:(NSInvocation *)invocation
repeats:(BOOL)repeats
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds
target:(id)target
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats
If seconds is less than or equal to 0.0, this method chooses a nonnegative interval. If repeats is YES, the timer will repeatedly reschedule itself.
- (void)fire
- (NSDate *)fireDate
- (void)invalidate
- (BOOL)isValid
- (NSTimeInterval)timeInterval
- (id)userInfo
See Also: + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:, + timerWithTimeInterval:target:selector:userInfo:repeats: