I've got something like this:
if (something) {
init();
} else {
document.addEventListener('CUSTOM_EVENT', init, false);
}
var init = function() {
document.removeEventListener('CUSTOM_EVENT', init, false);
// do stuff
}
Do I need to add some kind of check around that removeEventListener
call? If so, I could do:
var eventAdded = false;
if (something) {
init();
} else {
document.addEventListener('CUSTOM_EVENT', init, false);
eventAdded = true;
}
var init = function() {
if (eventAdded) {
document.removeEventListener('CUSTOM_EVENT', init, false);
}
// do stuff
}
but that feels a bit inelegant. I'd rather just leave the raw removeEventListener
call if that's no big deal, or do something that doesn't require having an external var if possible.
https://developer.mozilla.org/en/DOM/element.removeEventListener
Calling removeEventListener() with arguments which do not identify any currently registered EventListener on the EventTarget has no effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With