Class: NSTimer

Inherits:
NSObject show all

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)

Instance Method Summary (collapse)

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.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.

  • invocation (NSInvocation)

    The invocation to use when the timer fires. The timer instructs the invocation object to retain its arguments.

  • repeats (Boolean)

    If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

Returns:

  • (NSTimer)

    A new NSTimer object, configured according to the specified parameters.

+ (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.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.

  • target (Object)

    The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.

  • aSelector (Symbol)

    The message to send to target when the timer fires. The selector must correspond to a method that returns void and takes a single argument. The timer passes itself as the argument to this method.

  • userInfo (Object)

    The user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.

  • repeats (Boolean)

    If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

Returns:

  • (NSTimer)

    A new NSTimer object, configured according to the specified parameters.

+ (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.)

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead

  • invocation (NSInvocation)

    The invocation to use when the timer fires. The timer instructs the invocation object to retain its arguments.

  • repeats (Boolean)

    If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

Returns:

  • (NSTimer)

    A new NSTimer object, configured according to the specified parameters.

+ (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.)

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.

  • target (Object)

    The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.

  • aSelector (Symbol)

    The message to send to target when the timer fires. The selector must have the following signature:- (void)timerFireMethod:(NSTimer*)theTimerThe timer passes itself as the argument to this method.

  • userInfo (Object)

    Custom user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.

  • repeats (Boolean)

    If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

Returns:

  • (NSTimer)

    A new NSTimer object, configured according to the specified parameters.

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.

Returns:

- (NSDate) fireDate

Returns the date at which the receiver will fire. Use the isValid method to verify that the timer is valid.

Returns:

  • (NSDate)

    The date at which the receiver will fire. If the timer is no longer valid, this method returns the last date at which the timer fired.

- (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.)

Parameters:

  • date (NSDate)

    The time at which the timer should first fire.

  • seconds (NSTimeInterval)

    For a repeating timer, this parameter contains the number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead.

  • target (Object)

    The object to which to send the message specified by aSelector when the timer fires. The target object is retained by the timer and released when the timer is invalidated.

  • aSelector (Symbol)

    The message to send to target when the timer fires. The selector must have the following signature:- (void)timerFireMethod:(NSTimer*)theTimerThe timer passes itself as the argument to this method.

  • userInfo (Object)

    Custom user info for the timer. The object you specify is retained by the timer and released when the timer is invalidated. This parameter may be nil.

  • repeats (Boolean)

    If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.

Returns:

  • (Object)

    The receiver, initialized such that, when added to a run loop, it will fire at date and then, if repeats is YES, every seconds after that.

- (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.

Returns:

- (Boolean) isValid

Returns a Boolean value that indicates whether the receiver is currently valid.

Returns:

  • (Boolean)

    YES if the receiver is still capable of firing or NO if the timer has been invalidated and is no longer capable of firing.

- (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.

Parameters:

  • date (NSDate)

    The new date at which to fire the receiver. If the new date is in the past, this method sets the fire time to the current time.

Returns:

- (NSTimeInterval) timeInterval

Returns the receiver’s time interval.

Returns:

  • (NSTimeInterval)

    The receiver’s time interval. If the receiver is a non-repeating timer, returns 0 (even if a time interval was set).

- (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.

Returns:

  • (Object)

    The receiver's userInfo object.