When a button is clicked I want the event listener to be removed.
function clickMe(f,i){ ipcRenderer.send('click', i) ipcRenderer.on(`message`, function (en, message) { document.getElementById(`status${i}`).innerHTML = message[1]; //ipcRenderer.removeAllListeners('message') <- I don't want all listeners to be removed }); }
As shown in the code above, removeAllListeners works, however, I do not want all listeners to be removed. I believe I can use removeListener('message', function)
but if I try to put removeListener('message', startMe())
I get thrown an error of undefined. How would I go abouts doing this?
Advertisement
Answer
The method is called removeEventListener
which takes the function you want to remove as its second parameter.
You can’t remove it, because the function is anonymous. If you use named functions, it should work:
function onMessage = function (en, message) { document.getElementById(`status${i}`).innerHTML = message[1]; ipcRenderer.removeEventListener('message', onMessage); } function clickMe(f,i){ ipcRenderer.send('click', i) ipcRenderer.on('message', onMessage); }