Platforms to show: All Mac Windows Linux Cross-Platform
NSTimerMBS class
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
class | Cocoa | MBS MacCocoa Plugin | 10.0 | ✅ Yes | ❌ No | ❌ No | ✅ Yes | All |
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, calling the action event. For example, you could create an NSTimer object that calls the action event, telling it to update itself after a certain time interval.
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer's firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer's firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
Repeating Versus Non-Repeating Timers
You specify whether a timer is repeating or non-repeating at creation time. A non-repeating timer fires once and then invalidates itself automatically, thereby preventing the timer from firing again. By contrast, a repeating timer fires and then reschedules itself on the same run loop.
A repeating timer always schedules itself based on the scheduled firing time, as opposed to the actual firing time. For example, if a timer is scheduled to fire at a particular time and every 5 seconds after that, the scheduled firing time will always fall on the original 5 second time intervals, even if the actual firing time gets delayed. If the firing time is delayed so far that it passes one or more of the scheduled firing times, the timer is fired only once for that time period; the timer is then rescheduled, after firing, for the next scheduled firing time in the future.
Scheduling Timers in Run Loops
A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:
Use the Constructor to create the timer and schedule it on the current run loop in the default mode.
Once scheduled, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes and releases the timer, either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.
- event Action
- 7 properties
- property fireDate as date
- property fireDateTime as DateTime
- property Handle as Integer
- property isValid as boolean
- property tag as Variant
- property timeInterval as Double
- property tolerance as Double
- 7 methods
- method Constructor(fireDate as date, timeInterval as Double, repeats as boolean)
- method Constructor(fireDate as date, timeInterval as Double, repeats as boolean, runloop as NSRunLoopMBS, runloopMode as string)
- method Constructor(fireDate as dateTime, timeInterval as double, repeats as boolean)
- method Constructor(fireDate as dateTime, timeInterval as double, repeats as boolean, runloop as NSRunLoopMBS, runloopMode as string)
- method Constructor(timeInterval as Double, repeats as boolean)
- method fire
- method invalidate
- shared method Timer(t as timer) as NSTimerMBS
This class has no sub classes.
Some examples using this class:
Blog Entries
- MBS Xojo / Real Studio plug-ins in version 13.5
- MBS Xojo / Real Studio Plugins, version 13.5pr1
- MBS REALbasic plug-ins version 10.0
Xojo Developer Magazine
The items on this page are in the following plugins: MBS MacCocoa Plugin.