Class: NSTimer
Overview
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.
Class Method Summary (collapse)
-
+ scheduledTimerWithTimeInterval:invocation:repeats:
Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
-
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
-
+ timerWithTimeInterval:invocation:repeats:
Creates and returns a new NSTimer object initialized with the specified invocation object.
-
+ timerWithTimeInterval:target:selector:userInfo:repeats:
Creates and returns a new NSTimer object initialized with the specified object and selector.
Instance Method Summary (collapse)
-
- fire
Causes the receiver’s message to be sent to its target.
-
- fireDate
Returns the date at which the receiver will fire.
-
- initWithFireDate:interval:target:selector:userInfo:repeats:
Initializes a new NSTimer object using the specified object and selector.
-
- invalidate
Stops the receiver from ever firing again and requests its removal from its run loop.
-
- isValid
Returns a Boolean value that indicates whether the receiver is currently valid.
-
- setFireDate:
Resets the firing time of the receiver to the specified date.
-
- timeInterval
Returns the receiver’s time interval.
-
- userInfo
Returns the receiver's userInfo object.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (NSTimer) scheduledTimerWithTimeInterval(seconds, invocation:invocation, repeats:repeats)
Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode. After seconds seconds have elapsed, the timer fires, invoking invocation.
+ (NSTimer) scheduledTimerWithTimeInterval(seconds, target:target, selector:aSelector, userInfo:userInfo, repeats:repeats)
Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode. After seconds seconds have elapsed, the timer fires, sending the message aSelector to target.
+ (NSTimer) timerWithTimeInterval(seconds, invocation:invocation, repeats:repeats)
Creates and returns a new NSTimer object initialized with the specified invocation object. You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds have elapsed, the timer fires, invoking invocation. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
+ (NSTimer) timerWithTimeInterval(seconds, target:target, selector:aSelector, userInfo:userInfo, repeats:repeats)
Creates and returns a new NSTimer object initialized with the specified object and selector. You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds seconds have elapsed, the timer fires, sending the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
Instance Method Details
- (Object) fire
Causes the receiver’s message to be sent to its target. You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.
- (NSDate) fireDate
Returns the date at which the receiver will fire. Use the isValid method to verify that the timer is valid.
- (Object) initWithFireDate(date, interval:seconds, target:target, selector:aSelector, userInfo:userInfo, repeats:repeats)
Initializes a new NSTimer object using the specified object and selector. You must add the new timer to a run loop, using addTimer:forMode:. Upon firing, the timer sends the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
- (Object) invalidate
Stops the receiver from ever firing again and requests its removal from its run loop. This method is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes and releases the timer, either just before the invalidate method returns or at some later point.If it was configured with target and user info objects, the receiver releases its references to those objects as well.
- (Boolean) isValid
Returns a Boolean value that indicates whether the receiver is currently valid.
- (Object) setFireDate(date)
Resets the firing time of the receiver to the specified date. You typically use this method to adjust the firing time of a repeating timer. Although resetting a timer’s next firing time is a relatively expensive operation, it may be more efficient in some situations. For example, you could use it in situations where you want to repeat an action multiple times in the future, but at irregular time intervals. Adjusting the firing time of a single timer would likely incur less expense than creating multiple timer objects, scheduling each one on a run loop, and then destroying them.You should not call this method on a timer that has been invalidated, which includes non-repeating timers that have already fired. You could potentially call this method on a non-repeating timer that had not yet fired, although you should always do so from the thread to which the timer is attached to avoid potential race conditions.
- (NSTimeInterval) timeInterval
Returns the receiver’s time interval.
- (Object) userInfo
Returns the receiver's userInfo object. Do not invoke this method after the timer is invalidated. Use isValid to test whether the timer is valid.