0

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?

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.