timersPromises.setInterval([delay[, value[, options]]])
返回异步迭代器,以 delay
毫秒的间隔生成值。
delay
<number> 迭代之间等待的毫秒数。 默认值:1
。value
<any> 迭代器返回的值。options
<Object>ref
<boolean> 设置为false
以指示迭代之间的调度的Timeout
不应要求 Node.js 事件循环保持活动状态。 默认值:true
。signal
<AbortSignal> 可选的AbortSignal
,可用于在操作之间取消调度的Timeout
。
import {
setInterval,
} from 'timers/promises';
const interval = 100;
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)
break;
}
console.log(Date.now());
const {
setInterval,
} = require('node:timers/promises');
const interval = 100;
(async function() {
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)
break;
}
console.log(Date.now());
})();
Returns an async iterator that generates values in an interval of delay
ms.
delay
<number> The number of milliseconds to wait between iterations. Default:1
.value
<any> A value with which the iterator returns.options
<Object>ref
<boolean> Set tofalse
to indicate that the scheduledTimeout
between iterations should not require the Node.js event loop to remain active. Default:true
.signal
<AbortSignal> An optionalAbortSignal
that can be used to cancel the scheduledTimeout
between operations.
import {
setInterval,
} from 'timers/promises';
const interval = 100;
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)
break;
}
console.log(Date.now());
const {
setInterval,
} = require('node:timers/promises');
const interval = 100;
(async function() {
for await (const startTime of setInterval(interval, Date.now())) {
const now = Date.now();
console.log(now);
if ((now - startTime) > 1000)
break;
}
console.log(Date.now());
})();