DOM
The when() method of the EventTarget interface returns an Observable object representing a stream of events fired on the event target it is called on.
You can call Observable.subscribe on the resulting observable to subscribe to the event stream.
The returned observable uses an event listener in the background, the same as listeners created by EventTarget.addEventListener(). This listener's creation, sharing among observers, and removal follow the same lifecycle as the Subscriber object for a custom observable.
[!NOTE] This shared-subscription behavior may change. A proposal to give each observer its own
Subscriberwould make each subscription start a separate execution instead of reusing an active subscription.
Syntax
when(type)
when(type, options)
Parameters
type- : A case-sensitive string representing the event type to listen for.
options- : An object that specifies characteristics about the event listener. The available options are:
capture- : A boolean value indicating that events of the specified type will be dispatched to the listener registered in the background before being dispatched to any
EventTargetbeneath it in the DOM tree. If not specified, defaults tofalse.
- : A boolean value indicating that events of the specified type will be dispatched to the listener registered in the background before being dispatched to any
passive-
: A boolean value that, if
true, indicates that callbacks processing the events will never call Event.preventDefault. If a passive listener callspreventDefault(), nothing will happen and a console warning may be generated.If this option is not specified, it defaults to the same value as for
addEventListener(). See Using passive listeners to learn more.
-
- : An object that specifies characteristics about the event listener. The available options are:
Return value
An Observable.
Examples
Using when()
This example is a simple click counter app.
HTML
The markup contains a button element to click, and a p element to display the number of clicks.
<button>Click me</button>
<p>Click count: 0</p>
JavaScript
We call when("click") on the btn, which obtains an observable for the click event stream. We then chain a subscribe() call onto the observable to subscribe the increment() function to the observable, so it is called each time the button is clicked.
const btn = document.querySelector("button");
const para = document.querySelector("p");
let countValue = 0;
function increment() {
countValue++;
para.textContent = `Click count: ${countValue}`;
}
btn.when("click").subscribe(increment);
[!NOTE] For this particular example,
.when("click").subscribe(increment)and.addEventListener("click", increment)are almost exactly equivalent. However, usingwhen()allows you to compose event streams using observables. The Using observables guide has more examples.
Result
The rendered result is as follows:
basic-when
Try clicking the button to see the click count increment.
Specifications
Browser compatibility
See also
- Using observables
- EventTarget.addEventListener()
- Observable explainer
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
