Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unregister events in electron?

Tags:

electron

I am making an application using atom-shell that needs to load several html pages. Each time I load a different page, I need to execute some custom script. For this I am using mainWindow.webContents.on('did-finish-load', ...). But since I need to have a custom function for each file, I would like to unregister from the did-finish-load event.

Example:

mainWindow.webContents.on('did-finish-load',function() {
    console.log('loaded page1');
    mainWindow.webContents.unregister('did-finish-load') // <= does that exist?
});
mainWindow.loadUrl('file://.../page1.html');
like image 613
edeboursetty Avatar asked Mar 28 '15 18:03

edeboursetty


1 Answers

BrowserWindow extends EventEmitter, so you would remove a listener the same way you typically would in Node.js:

var handler = function () {
    // ...
};
mainWindow.webContents.on('did-finish-load', handler);

// Later:
mainWindow.webContents.removeListener('did-finish-load', handler);
like image 195
Ken Franqueiro Avatar answered Oct 20 '22 12:10

Ken Franqueiro