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. |
方法
initWithCallback()
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
-
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()
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
-
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()
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
-
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 thensITimer
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.
实例
// 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);
标记: