简体   繁体   中英

Electron - SetTimeout for ipcRenderer.send()

Hi I'm calling the ipcRenderer.send() from the index.html file.

What i'm trying to do is to delay the function call by 5 seconds. However, it doesn't seem to work.

Here's what I'm trying to do essentially:

setTimeout(ipcRenderer.send('startYellowApple', ["bob", "cat"]), 5000);

and here's how I execute this in a regular Native Javascript function:

function start(){
  setTimeout(ipcRenderer.send('startYellowApple', ["bob", "cat"]), 5000);
}

Am I doing something wrong?

However I manage to make some progress by using this method.. However it freezes up my entire electron program until the 5 seconds is over which is really not ideal:

function sleep(milliseconds) {
  const date = Date.now();
   let currentDate = null;
   do {
     currentDate = Date.now();
   } while (currentDate - date < milliseconds);
}

function start(){
  sleep(5000);
  ipcRenderer.send('startYellowApple', ["bob", "cat"]);
}

Could someone help me out please? Thanks!

The problem is how you pass your callback to setTimeout function.

ipcRenderer.send('startYellowApple', ["bob", "cat"])

is actually a function call, it will invoke immediately, you should wrap the call inside a function to achieve what you want.

function start(){
   setTimeout(() => ipcRenderer.send('startYellowApple', ["bob", "cat"]), 5000);
}

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