1

I have one page, that load new elements via jquery. I have page that have event listener like

ipcRenderer.send('getlist');
ipcRenderer.once('return:list', function (e, l, wn) {
    console.log('a');
    for(let i = 0; i < l.length; i++) {
        $('.a').append('<div>'+l[i]+wn[i]'</div>');
    }
    $('.a').append('<div>End</div>');
});

Code that send data to page:

ipcMain.on('getlist', function (e) {
    wn = [];
    cfg['a'].forEach(s => {
        wn.push(yaml.readSync('charcfg.yaml')['name']);
    });
    mainWindow.webContents.send('return:list', cfg['a'], wn);
});

And it's works perfectly until I load another page, and again load this page. This event fires multiple times. Each time I come back to this page, more times it goes.

I tried to use

ipcRenderer.once('someevent', function(e, l){...}); 

It's works only ONE TIME. On third reload it's starts do listener multiple times again.

Code with this script loads with page!

Sorry for my english.Output

Function that loads page via jquery:

const htmlContent = $('.content');
function setContent(s) {
    $('.content').animate({
        opacity: 0
    }, 200, function () {
        setTimeout(function () {
            htmlContent.load(s + '.html');
            $('.content').animate({
                opacity: 1
            }, 200);
        }, 100);
    });
}
SoundOfTheSky
  • 99
  • 1
  • 10

1 Answers1

4

Actually there was TWO listeners that go twice.

It's ipcRenderer.on (replaced to .once) and jquery.on('click') that was bound to button that loads page. Fixed this with jquery.unbind().on('click').

Thanks for your help, obermillerk.

pushkin
  • 9,575
  • 15
  • 51
  • 95
SoundOfTheSky
  • 99
  • 1
  • 10
  • Just wanted to add that in my case I've included `renderer.js` in my html manually and it was already getting included by `main.js` so that's why it fired twice for me. – Miro Sep 29 '21 at 19:14