简体   繁体   中英

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

Demand:

The renderer process need send data to main process.

My code:

//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)
  }
}

Question I get the error:

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

Semms cannt use ipc in setInterval ?

How can i do this..

Thanks!

There's no need to define another function inside the class WebWindow, nor do you need to redeclare this inside of arrow functions.

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

class WebWindow {
  constructor() {

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM