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


  • callback <Function> 定时器到期时要调用的函数。
  • delay <number> 调用 callback 之前要等待的毫秒数。默认值: 1
  • ...args <any> 调用 callback 时要传递的可选参数。
  • 返回值:<Timeout>,用于 clearTimeout()

delay 毫秒后安排一次性 callback 的执行。

【Schedules execution of a one-time callback after delay milliseconds.】

callback 很可能不会在准确的 delay 毫秒后被调用。Node.js 对回调触发的确切时间不作保证,也不保证它们的顺序。回调会尽可能接近指定的时间被调用。

【The callback will likely not be invoked in precisely delay milliseconds. Node.js makes no guarantees about the exact timing of when callbacks will fire, nor of their ordering. The callback will be called as close as possible to the time specified.】

delay 大于 2147483647 或小于 1 时,delay 将被设置为 1。非整数的延迟会被截断为整数。

【When delay is larger than 2147483647 or less than 1, the delay will be set to 1. Non-integer delays are truncated to an integer.】

如果 callback 不是一个函数,将会抛出 TypeError

【If callback is not a function, a TypeError will be thrown.】

这种方法有一个适用于 Promise 的自定义变体,可以通过 timersPromises.setTimeout() 使用。

【This method has a custom variant for promises that is available using timersPromises.setTimeout().】