WorkerGlobalScope: scheduler 属性
注意:此功能仅在 Web Workers 中可用。
WorkerGlobalScope 接口的只读属性 scheduler 是使用 优先级任务调度 API 的入口点。
它返回一个包含 postTask() 和 yield() 方法的 Scheduler 对象实例,这些方法可用于调度优先级任务。
值
一个 Scheduler。
示例
下面的代码展示了该属性及其相关接口的一个非常基本的使用方法。它演示了如何检查属性是否存在,然后发布一个返回 Promise 的任务。
js
// Check if the prioritized task API is supported
if ("scheduler" in self) {
// Callback function - "the task"
const myTask = () => "Task 1: user-visible";
// Post task with default priority: 'user-visible' (no other options)
// When the task resolves, Promise.then() logs the result.
self.scheduler
.postTask(myTask)
// Handle resolved value
.then((taskResult) => console.log(`${taskResult}`))
// Handle error or abort
.catch((error) => console.log(`Error: ${error}`));
} else {
console.log("Feature: NOT Supported");
}
有关展示如何使用该 API 的全面示例代码,请参阅 优先级任务调度 API > 示例。
规范
| 规范 |
|---|
| 优先任务调度 # dom-windoworworkerglobalscope-scheduler |
浏览器兼容性
加载中…