How to clear all added listeners from input?

I call the following code to update listeners whenever the user changes the MIDI input device.


for (let i = 0; i < midiInIds.length; i++) {

    function receiveNoteOn(e) {
    }

    let input = WebMidi.getInputById(midiInIds[i]);
    input.removeListener("noteon", "all", receiveNoteOn);
    input.addListener("noteon", "all", receiveNoteOn);

}

But I'm worried if any of the listeners of the previous setting won't be removed.

So I would like to remove all added listeners of the "WebMidi.inputs" before adding new ones.

How can I do this?

Best Answer

  • edited August 2020 Accepted Answer

    So I would like to remove all added listeners of the "WebMidi.inputs" before adding new ones.

    You can remove all listeners from a single input by calling removeListener() on that input without any arguments. If you want to remove all listeners from all inputs, you will have to loop through them.

    For details, check out the removeListener() documentation.