Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

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);

方法预览

Edit section
void initWithCallback(in nsITimerCallback aCallback, in unsigned long aDelay, in unsigned long aType);
[noscript] void initWithFuncCallback(in nsITimerCallbackFunc aCallback, in voidPtr aClosure, in unsigned long aDelay, in unsigned long aType);
void init(in nsIObserver aObserver, in unsigned long aDelay, in unsigned long aType);
void cancel();

属性

Edit section
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 nsITimerCallback The nsITimerCallback object passed to initWithCallback().
closure [noscript] readonly voidPtr

The opaque pointer pass to initWithFuncCallback().

target nsIEventTarget The nsIEventTarget to which the callback is dispatched.  This target must be set before calling any of the initialization methods.

常量

Edit section
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.

方法

Edit section

initWithCallback()

Edit section

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
 );
Parameters
Edit section
aFunc
nsITimerCallback interface to call when timer expires.
aDelay
timeout delay in milliseconds.
aType
Defines the timer type: one shot, repeating slack or repeating precise. Must be one of the constants defined under Constants on this page.

initWithFuncCallback()

Edit section

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
 );
Parameters
Edit section
aFunc
a nsITimerCallbackFunc interface compatible function to call when timer fires.
aClosure
An opaque pointer to pass to that function.
aDelay
timeout delay in milliseconds.
aType
Defines the timer type: one shot, repeating slack or repeating precise. Must be one of the constants defined under Constants on this page.

init()

Edit section

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
 );
Parameters
Edit section
aObserver
A callback Object, that is capable listening to timer events. If the timer fires, the observer will be notified via the 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
timeout delay in milliseconds.
aType
Defines the timer type: one shot, repeating slack or repeating precise. Must be one of the constants defined under Constants on this page.

cancel()

Edit section

Cancels the timer. This method works on all types, not just on repeating timers -- you might want to cancel a TYPE_ONE_SHOT timer, and even reuse it by re-initializing it (to avoid object destruction and creation costs by conserving one timer instance).

 void cancel();
Parameters
Edit section

None.

备注

Edit section

TYPE_REPEATING_SLACK timer is the preferable repeating timer type for most situations

 

实例
Edit section
 // 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);

 

标记:

 

文档标签和贡献者

 此页面的贡献者: ziyunfei, hanyuxinting
 最后编辑者: ziyunfei,