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

TaskSignal: prioritychange event

· 52CE 我爱测

The **`prioritychange`** event is sent to a TaskSignal if its priority is changed.

Prioritized Task Scheduling API

The prioritychange event is sent to a TaskSignal if its priority is changed.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("prioritychange", (event) => { })

Alternatively, assign to the event handler property of the interface:

onprioritychange = (event) => { }

Event type

A TaskPriorityChangeEvent. Inherits from Event.

TaskPriorityChangeEvent

Examples

The example below shows how to listen for the prioritychange event on a TaskSignal.

<textarea id="log"></textarea>
#log {
  min-height: 70px;
  width: 95%;
}
let log = document.getElementById("log");
function myLog(text) {
  log.textContent += `${text}\n`;
}

First we create a controller, and add an event listener to its signal. When handling the event we use TaskPriorityChangeEvent.previousPriority on the event to get the original priority and TaskSignal.priority on the event target to get the new/current priority.

The task is then posted, passing in the signal, and then we immediately change the priority.

if ("scheduler" in this) {
  // Declare a TaskController, setting its signal priority to 'user-blocking'
  const controller = new TaskController({ priority: "user-blocking" });

  // Listen for 'prioritychange' events on the controller's signal.
  controller.signal.addEventListener("prioritychange", (event) => {
    const previousPriority = event.previousPriority;
    const newPriority = event.target.priority;
    myLog(`Priority changed from ${previousPriority} to ${newPriority}.`);
  });

  // Post task using the controller's signal.
  // The signal priority sets the initial priority of the task
  scheduler.postTask(
    () => {
      myLog("Task 1");
    },
    { signal: controller.signal },
  );

  // Change the priority to 'background' using the controller
  controller.setPriority("background");
}

[!NOTE] The code above uses a custom logging function myLog() to log to the text area below. This is hidden as it isn't relevant to the example.

The output below demonstrates shows that the task's priority changed from user-blocking to background. This happens before the task is executed, but could also happen when the task is running.

Examples

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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