This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 28455 - Add a method to add a one-time event listener
Summary: Add a method to add a one-time event listener
Status: RESOLVED MOVED
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: Other All
: P2 normal
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
URL:
Whiteboard: pending new event API
Keywords:
Depends on:
Blocks:
 
Reported: 2015-04-09 21:38 UTC by Xidorn Quan
Modified: 2016-04-08 11:26 UTC (History)
5 users (show)

See Also:


Attachments

Description Xidorn Quan 2015-04-09 21:38:06 UTC
It seems to be a common pattern to have code like:

element.addEventListener("click", function onClick(event) {
  element.removeEventListener("click", onClick);
  ...
});

jQuery has method ".one()" to simplify this pattern.

I think it would be great if DOM directly includes a method for this in EventTarget. Probably this method could be:

void listenForNextEvent(DOMString type, EventListener? callback, optional boolean capture = false);
Comment 1 Sebastian Zartner 2015-04-20 07:26:50 UTC
> Probably this method could be:
> 
> void listenForNextEvent(DOMString type, EventListener? callback, optional 
> boolean capture = false);

Or the existing method could be extended by a fourth parameter:

void addEventListener(DOMString type, EventListener? callback, optional boolean capture = false, optional boolean once = false)

Sebastian
Comment 2 Arkadiusz Michalski (Spirit) 2015-06-12 07:44:11 UTC
I'm vote for this because do this a lot of time... and will be nice if this new stuff can remove anonymous callback too.
Comment 3 Anne 2016-03-14 13:16:47 UTC
Closing this for now. If we do this we should have a new better event API in general, that addresses a number of concerns. https://gist.github.com/annevk/4475457 has a sketch of such a thing, if you're interested.
Comment 4 Anne 2016-04-08 11:26:04 UTC
This is now https://github.com/whatwg/dom/pull/207. I found a way to do this now addEventListener() takes options. Would love your feedback.