Window:scheduler 属性
Window 接口的只读属性 scheduler 是使用 优先任务调度 API 的入口点。
它返回一个 Scheduler 对象实例,其中包含可用于调度优先任务的 postTask() 和 yield() 方法。
值
一个 Scheduler。
示例
下面的代码展示了该属性及其相关接口的一个非常基础的用法。它演示了如何检查该属性是否存在,然后发布一个返回 Promise 的任务。
js
// Check if the prioritized task API is supported
if ("scheduler" in window) {
// 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.
window.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 |
浏览器兼容性
加载中…