// Create a TaskController, setting its signal priority to 'user-blocking'
const controller = new TaskController({ priority: "user-blocking" });TaskController / notebook
TaskController
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
// Listen for 'prioritychange' events on the controller's signal.
controller.signal.addEventListener("prioritychange", (event) => {
const previousPriority = event.previousPriority;
const newPriority = event.target.priority;
console.log(`Priority changed from ${previousPriority} to ${newPriority}.`);
});controller.signal.addEventListener(
"abort",
(event) => {
console.log("Task aborted");
},
{ once: true },
);// Post task using the controller's signal.
// The signal priority sets the initial priority of the task
scheduler
.postTask(() => "Task execute", { signal: controller.signal })
.then((taskResult) => {
console.log(`${taskResult}`);
}) // Aborted (won't run)
.catch((error) => {
console.log(`Catch error: ${error}`);
}); // Log error// Change the priority to 'background' using the controller
controller.setPriority("background");// Abort the task
controller.abort();