Next Page > Hide TOC

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

NSTimer

Inherits from
Package
com.apple.cocoa.foundation
Companion guides

Overview

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.

If you specify in the constructor that the timer should repeat, it automatically reschedules itself after it fires. If you specify that the timer should not repeat, it is automatically invalidated after it fires.

To request the removal of a timer from an NSRunLoop, send the timer the invalidate message from the same thread on which the timer was installed. This message immediately disables the timer, so it no longer affects the NSRunLoop. The NSRunLoop removes and releases the timer, either just before the invalidate method returns or at some later point.

Tasks

Constructors

Stopping a Timer

Information About a Timer

Constructors

NSTimer

Returns an empty NSTimer.

public NSTimer()

Creates a new NSTimer that, when added to a run loop, will fire after seconds.

public NSTimer(double seconds, Object target, NSSelector aSelector, Object userInfo, boolean repeats)

Discussion

Upon firing, the timer sends aSelector to target. The aSelector method must have the following syntax:

public void myTimerFireMethod(NSTimer* theTimer)

The timer passes itself as the argument to aSelector. To pass more information to the target, use userInfo. The target gets userInfo by sending userInfo to the timer.

If seconds is less than or equal to 0.0, this method chooses a nonnegative interval. If repeats is true, the timer will repeatedly reschedule itself until invalidated. If repeats is false, the timer will be invalidated after it fires.

Instance Methods

invalidate

Stops the receiver from ever firing again and requests its removal from its NSRunLoop.

public void invalidate()

Discussion

This is the only way to remove a timer from an NSRunLoop. The NSRunLoop removes and releases the timer, either just before the invalidate method returns or at some later point.

You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

isValid

Returns true if the receiver is currently valid, false otherwise.

public boolean isValid()

timeInterval

Returns the receiver’s time interval.

public double timeInterval()

userInfo

Returns the userInfo object, containing additional data the target may use when the receiver is fired.

public Object userInfo()

Discussion

Do not invoke this method after the timer is invalidated. Use isValid to test whether the timer is valid.



Next Page > Hide TOC


© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)


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.