大网数据广告活动
Web 标准与前端开发

EventTarget: when() method

· 52CE 我爱测

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.

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 Subscriber would 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 EventTarget beneath it in the DOM tree. If not specified, defaults to false.
      • passive
        • : A boolean value that, if true, indicates that callbacks processing the events will never call Event.preventDefault. If a passive listener calls preventDefault(), 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.

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, using when() 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

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。