The prioritychange
event is sent to a TaskSignal
if its priority is changed.
The prioritychange
event is sent to a TaskSignal
if its priority is changed.
Use the event name in methods like addEventListener()
, or set an event handler property.
js
addEventListener("prioritychange", (event) => {}); onprioritychange = (event) => {};
A TaskPriorityChangeEvent
. Inherits from Event
.
TaskPriorityChangeEvent.previousPriority
Indicates the previous priority of the task (before it was changed). The new/updated priority is read from event.target.priority
(TaskSignal.priority
).
The example below shows how to listen for the prioritychange
event on a TaskSignal
.
First we create a controller, and add an event listener to its signal. When handling the event we use 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.
js
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.
Specification |
---|
Prioritized Task Scheduling # ref-for-eventdef-tasksignal-prioritychange |
Prioritized Task Scheduling # dom-tasksignal-onprioritychange |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
prioritychange_event |
94 | 94 | 101 | No | 80 | No | 94 | 94 | No | 66 | No | 17.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TaskSignal/prioritychange_event