Say I add a load
event to the window like so:
window.addEventListener("load",initialize);
Should I then remove the load event listener from the window after the event is fired? It only fires once, but will it continue to listen after that happens?
It's simple enough to do:
function initialize(event_){
/* Just by adding this line. */
window.removeEventListener("load",initialize);
}
But is that overkill or will that actually benefit the performance of my program? I only ask because the "load" event only fires once, so it would make sense if it just resolved itself. I've never heard of a self resolving listener, though... Any thoughts?
Edit: Also, I'm not concerned specifically with the "load" event, just the general scenario where the listener continues to listen for an event that only fires once.
Say I add a load
event to the window like so:
window.addEventListener("load",initialize);
Should I then remove the load event listener from the window after the event is fired? It only fires once, but will it continue to listen after that happens?
It's simple enough to do:
function initialize(event_){
/* Just by adding this line. */
window.removeEventListener("load",initialize);
}
But is that overkill or will that actually benefit the performance of my program? I only ask because the "load" event only fires once, so it would make sense if it just resolved itself. I've never heard of a self resolving listener, though... Any thoughts?
Edit: Also, I'm not concerned specifically with the "load" event, just the general scenario where the listener continues to listen for an event that only fires once.
Share Improve this question edited Feb 12, 2021 at 15:17 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Apr 5, 2015 at 15:21 FrankFrank 2,2306 gold badges28 silver badges42 bronze badges 4 |2 Answers
Reset to default 21window.addEventListener('load', initialize, {once: true});
Should I then remove the load event listener from the window after the event is fired?
I've never seen that done, so I don't think there's a real need for it.
It only fires once, but will it continue to listen after that happens?
It is only fired once by the DOM, yes. But it will continue to listen, you could easily trigger a load
event manually (see MDN for examples).
But is that overkill or will that actually benefit the performance of my program?
Typically it's overkill, as this doesn't make a huge difference. Of course, it might trigger garbage collection on initialize
, which could save a bit of memory (or more, depending on your code structure) and improve performance by making it available to the rest of your app.
window.onload=initialize;
This will call your initialize function but won't continue as an eventlistener. I can't see any reason for removing the load event listener but i'm just giving another option/method but both will do the same thing. – NewToJS Commented Apr 5, 2015 at 15:28onload
property ofwindow
is just another way of establishing an event handler.) – Pointy Commented Apr 5, 2015 at 15:28