setInterval 中的电子渲染器进程不能使用 ipcRenderer.send()

Electron renderer process in setInterval cannt use ipcRenderer.send()

需求:

渲染进程需要向主进程发送数据。

我的代码:

//index.js (renderer process)
const {ipcRenderer} = require('electron')

class WebWindow {
  constructor() {

    ...

    setInterval(() => {
      this.foo()
    }, 2000)

    // or 

    let that = this
    setInterval(function() {
      thar.foo()
    }, 2000)


  }

  foo () {
    data = {}
    ipcRenderer.send('async-cookies', data)
  }
}

问题 我收到错误:

Uncaught Exception:
TypeError: Cannot read property 'send' of undefined
    at Function.eval

Semms 不能在 setInterval 中使用 ipc 吗?

我该怎么做..

谢谢!

无需在 class WebWindow 中定义另一个函数,也无需在箭头函数中重新声明 this

//index.js (renderer process)
const {ipcRenderer} = require('electron')

class WebWindow {
  constructor() {

    setInterval(() => {
      ipcRenderer.send('async-cookies', data)
    }, 2000)
  }
}