Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js a timer


May 10, 2021 Node.js


Table of contents


Timer

稳定性: 5 - 锁定

Node .js timer module provides a global API for calling functions at a later time period.

All timer functions are global. You do not need to be able to access it through require()

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

delay is executed after a delay callback R eturns timeoutObject object, which may clearTimeout() You can also pass arguments to callback functions.

It is important to note that your callback function may not be delay milliseconds, and node .js does not guarantee the exact time and order in which the callback function is executed. The callback function is as close as possible to the specified time.

clearTimeout(timeoutObject)

Block a timeout from being triggered.

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

Callback delay repeated every delay callback R eturns timeoutObject object, which may clearTimeout() You can also pass arguments to callback functions.

clearInterval(intervalObject)

Prevent an interval from being triggered.

unref()

setTimeout by setInterval with timer.unref() you to create an active timer, but it is in an event loop that will not keep the program running if it is left with only one timer. If the timer has already called unref call again will not be valid.

In setTimeout scenario, when you unref and create a stand-alone timer it wakes up the event loop. Creating too many of these things affects event loop performance, so use them with caution.

ref()

If you have unref() a timer, ref() explicitly request the timer to keep the program open. If the timer has already called ref() call again will not be valid.

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

Before setTimeout setInterval schedule a callback execution immediately after the input/output event.

The callbacks to the immediates are queued in the order in which they were created. T he entire callback queue is executed in the event loop iteration. If you add immediates to an executing callback, immediate will not be triggered until the next event loop iteration.

clearImmediate(immediateObject)

Used to stop the trigger of an immediate.