gpt4 book ai didi

javascript - Electron setTimeout 在渲染器进程中返回数字而不是 Timeout 对象

转载 作者:行者123 更新时间:2023-12-01 01:15:45 30 4
gpt4 key购买 nike

我注意到在Electron的渲染器进程中setTimeout Node 函数返回一个数字(整数)而不是超时对象。

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

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

.

渲染器进程的控制台:

enter image description here

主进程控制台:

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()window.setTimeout()是一个返回整数的 Web API 函数:

Return value

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

而在主进程中,setTimeout()是一个 Node.js 全局对象 方法(在 Timers 中描述),它返回 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);

关于javascript - Electron setTimeout 在渲染器进程中返回数字而不是 Timeout 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54783945/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com