May 10, 2021 Node.js
2. setTimeout(callback, delay[, arg][, ...])
3. clearTimeout(timeoutObject)
4. setInterval(callback, delay[, arg][, ...])
5. clearInterval(intervalObject)
稳定性: 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()
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.
Block a timeout from being triggered.
Callback
delay
repeated every delay
callback
R
eturns
timeoutObject
object, which may
clearTimeout()
You can also pass arguments to callback functions.
Prevent an interval from being triggered.
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.
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.
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.
Used to stop the trigger of an immediate.