知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


定时器#

稳定度: 5 - 已锁定

All of the timer functions are globals. You do not need to require() this module in order to use them.

所有的定时器函数都是全局变量. 你使用这些函数时不需要 require()模块.

setTimeout(callback, delay, [arg], [...])#

To schedule execution of a one-time callback after delay milliseconds. Returns a timeoutId for possible use with clearTimeout(). Optionally you can also pass arguments to the callback.

调度 delay 毫秒后的一次 callback 执行。返回一个可能被 clearTimeout() 用到的 timeoutId。可选地,您还能给回调传入参数。

It is important to note that your callback will probably not be called in exactly delay milliseconds - Node.js makes no guarantees about the exact timing of when the callback will fire, nor of the ordering things will fire in. The callback will be called as close as possible to the time specified.

请务必注意,您的回调有可能不会在准确的 delay 毫秒后被调用。Node.js 不保证回调被触发的精确时间和顺序。回调会在尽可能接近所指定时间上被调用。

clearTimeout(timeoutId)#

Prevents a timeout from triggering.

阻止一个 timeout 被触发。

setInterval(callback, delay, [arg], [...])#

To schedule the repeated execution of callback every delay milliseconds. Returns a intervalId for possible use with clearInterval(). Optionally you can also pass arguments to the callback.

调度每隔 delay 毫秒执行一次的 callback。返回一个可能被 clearInterval() 用到的 intervalId。可选地,您还能给回调传入参数。

clearInterval(intervalId)#

Stops a interval from triggering.

停止一个 interval 的触发。

unref()#

The opaque value returned by setTimeout and setInterval also has the method timer.unref() which will allow you to create a timer that is active but if it is the only item left in the event loop won't keep the program running. If the timer is already unrefd calling unref again will have no effect.

setTimeoutsetInterval 所返回的值同时具有 timer.unref() 方法,允许您创建一个活动的、但当它是事件循环中仅剩的项目时不会保持程序运行的定时器。如果定时器已被 unref,再次调用 unref 不会产生其它影响。

In the case of setTimeout when you unref you create a separate timer that will wakeup the event loop, creating too many of these may adversely effect event loop performance -- use wisely.

setTimeout 的情景中当您 unref 您会创建另一个定时器,并唤醒事件循环。创建太多这种定时器可能会影响事件循环的性能,慎用。

ref()#

If you had previously unref()d a timer you can call ref() to explicitly request the timer hold the program open. If the timer is already refd calling ref again will have no effect.

如果您之前 unref() 了一个定时器,您可以调用 ref() 来明确要求定时器让程序保持运行。如果定时器已被 ref 那么再次调用 ref 不会产生其它影响。

setImmediate(callback, [arg], [...])#

To schedule the "immediate" execution of callback after I/O events callbacks and before setTimeout and setInterval . Returns an immediateId for possible use with clearImmediate(). Optionally you can also pass arguments to the callback.

调度在所有 I/O 事件回调之后、setTimeoutsetInterval 之前“立即”执行 callback。返回一个可能被 clearImmediate() 用到的 immediateId。可选地,您还能给回调传入参数。

Callbacks for immediates are queued in the order in which they were created. The entire callback queue is processed every event loop iteration. If you queue an immediate from a inside an executing callback that immediate won't fire until the next event loop iteration.

immediate 的回调以它们创建的顺序被加入队列。整个回调队列会在每个事件循环迭代中被处理。如果您在一个正被执行的回调中添加 immediate,那么这个 immediate 在下一个事件循环迭代之前都不会被触发。

clearImmediate(immediateId)#

Stops an immediate from triggering.

停止一个 immediate 的触发。