最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I always removeEventListener? - Stack Overflow

programmeradmin1浏览0评论

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
  • If you're unsure about this method, why not try 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:28
  • 1 There's no reason to do that. (@NewToJS ?? setting the onload property of window is just another way of establishing an event handler.) – Pointy Commented Apr 5, 2015 at 15:28
  • @Pointy I understand that, i'm just sharing another method of triggering it but as you clearly pointed out, there isn't any reason for removing the event. – NewToJS Commented Apr 5, 2015 at 15:31
  • I'm not so concerned with the actual load event, just the specific scenario of removing the listener and whether or not a listener will continue listening for an event that only fires once after the fact. – Frank Commented Apr 5, 2015 at 15:42
Add a comment  | 

2 Answers 2

Reset to default 21

window.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.

发布评论

评论列表(0)

  1. 暂无评论