Electron setTimeout returns renderer 进程中的数字而不是 Timeout 对象

Electron setTimeout returns number in renderer process instead of Timeout object

我注意到 在 Electron 的渲染器进程中,setTimeout 节点函数 returns 一个 number (整数)而不是一个Timeout对象。

在主进程中,它returns预期的Timeout对象

我使用最新版本(即 4.0.5)。

.

渲染进程的控制台:

主进程的控制台:

npm start

> electron-timer-bug@0.0.1 start /home/doom/Documents/projets/testElectron/electron-timer-bug
> electron .

/home/doom/Documents/projets/testElectron/electron-timer-bug/node_modules/electron/dist/electron: /lib/x86_64-linux-gnu/libdbus-1.so.3: no version information available (required by /home/doom/Documents/projets/testElectron/electron-timer-bug/node_modules/electron/dist/electron)
/home/doom/Documents/projets/testElectron/electron-timer-bug/node_modules/electron/dist/electron: /lib/x86_64-linux-gnu/libdbus-1.so.3: no version information available (required by /home/doom/Documents/projets/testElectron/electron-timer-bug/node_modules/electron/dist/electron)
Fontconfig warning: "/etc/fonts/fonts.conf", line 86: unknown element "blank"
mainWatchdog :  Timeout {
  _called: false,
  _idleTimeout: 1000,
  _idlePrev:
   TimersList {
     _idleNext: [Circular],
     _idlePrev: [Circular],
     _unrefed: false,
     msecs: 1000,
     _timer: Timer { _list: [Circular] } },
  _idleNext:
   TimersList {
     _idleNext: [Circular],
     _idlePrev: [Circular],
     _unrefed: false,
     msecs: 1000,
     _timer: Timer { _list: [Circular] } },
  _idleStart: 648,
  _onTimeout: [Function],
  _timerArgs: undefined,
  _repeat: null,
  _destroyed: false,
  [Symbol(unrefed)]: false,
  [Symbol(asyncId)]: 7,
  [Symbol(triggerId)]: 5 }
typeof(mainWatchdog) :  object
mainWatchdog.constructor.name:  Timeout
main callback

有无nodeIntegration都一样

这是测试仓库:https://gitlab.com/doom-fr/electron-timer-bug

正常吗?怎么了?

厄运

渲染器进程 中,setTimeout() or window.setTimeout() 是一个 Web API 函数,其中 returns 一个整数:

Return value

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(); this value can be passed to clearTimeout() to cancel the timeout.

而在主进程setTimeout() is a Node.js global object method (described in Timers) which returns a Timeout对象中:

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

  • callback The function to call when the timer elapses.
  • delay The number of milliseconds to wait before calling the callback.
  • ...args Optional arguments to pass when the callback is called.

Returns: <Timeout> for use with clearTimeout()

为了从 渲染器进程 调用 Node.js 方法,您必须使用 Electron 的远程 getGlobal 方法;例如:

require('electron').remote.getGlobal('setTimeout')(() => { console.log('done'); }, 2000);