--- title: nsITimer slug: Mozilla/Tech/XPCOM/Reference/Interface/nsITimer translation_of: Mozilla/Tech/XPCOM/Reference/Interface/nsITimer ---
nsITimer 接口的功能是:在指定延迟后唤醒一个程序。
nsITimer
的定义文档: xpcom/threads/nsITimer.idl
。It is scriptable and unfrozen (hasn't changed since 1.9.1) .
继承自: nsISupports
实现: @mozilla.org/timer;1
。创建实例如:
var timer = Components.classes["@mozilla.org/timer;1"] .createInstance(Components.interfaces.nsITimer);
void initWithCallback(in |
[noscript] void initWithFuncCallback(in |
void init(in |
void cancel(); |
Attribute | Type | Description |
delay |
unsigned long |
The timeout delay in millisecond. |
type |
unsigned long |
Defines the timer type: one shot, repeating slack or repeating precise. Must be one of the constants defined under Constants on this page |
callback |
readonly |
The nsITimerCallback object passed to initWithCallback() . |
closure |
[noscript] readonly voidPtr |
The opaque pointer pass to |
target | nsIEventTarget |
The nsIEventTarget to which the callback is dispatched. This target must be set before calling any of the initialization methods. |
Constant | Value | Description |
TYPE_ONE_SHOT |
0 | Type of a timer that fires once only. |
TYPE_REPEATING_SLACK |
1 | After firing, the timer is stopped and not restarted until its callback completes. Specified timer period will be at least the time between when processing for last firing the callback completes and when the next firing occurs. |
TYPE_REPEATING_PRECISE |
2 | This repeating timer aims to have constant period between firings. The processing time for each timer callback should not influence the timer period. However, if the processing for the last timer firing could not be completed until just before the next firing occurs, then you could have two timer notification routines being executed in quick succession. |
Initialize a timer to fire after the given millisecond interval. This version takes a function to call and a closure to pass to that function.
void initWithCallback ( in nsITimerCallback aCallback, in unsigned long aDelay, in unsigned long aType );
aFunc
nsITimerCallback
interface to call when timer expires.aDelay
aType
Initialize a timer to fire after the given millisecond interval. This version takes a function to call and a closure to pass to that function.
[noscript] void initWithFuncCallback( in nsTimerCallbackFunc aCallback, in voidPtr aClosure, in unsigned long aDelay, in unsigned long aType );
aFunc
nsITimerCallbackFunc
interface compatible function to call when timer fires.aClosure
aDelay
aType
Initialize a timer that will fire after the specified delay. A user must keep a reference to this timer till it is no longer needed or has been canceled.
void init( in nsIObserver aObserver, in unsigned long aDelay, in unsigned long aType );
aObserver
nsIObserver
Interface. The subject is set to the nsITimer
Object which fired, the topic is equal to "timer-callback" and data is always set to null.aDelay
aType
// we need a nsITimerCallback
compatible...
// ... interface for the callbacks.
var event
= { notify: function(timer) { alert("Fire!"); } }
// Now it is time to create the timer...
var timer
= Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
// ... and to initialize it, we want to call event.notify() ...
// ... one time after exactly ten second.
timer.initWithCallback(
event,
10000,
Components.interfaces.nsITimer.TYPE_ONE_SHOT);
标记: