W3cubDocs

/Web APIs

Scheduler

The Scheduler interface of the Prioritized Task Scheduling API provides the Scheduler.postTask() method that can be used for adding prioritized tasks to be scheduled.

A Scheduler can be accessed from the global object Window or WorkerGlobalScope (this.scheduler).

Instance properties

None.

Instance methods

Scheduler.postTask()

Adds a task to the scheduler as a callback, optionally specifying a priority, delay, and/or a signal for aborting the task.

Examples

If the feature is defined, an instance of this object is returned by the global this in both workers and the main thread. The only property of the interface is the postTask() method, which is used to post the task and returns a promise.

The code below shows a simple task that resolves with the text 'Task executing'. This text is logged on success. The code also shows a catch block, which would be required in more complex code to handle when a task is aborted or throws an error.

js

if ("scheduler" in this) {
  // Post task with default priority: 'user-visible' (no other options)
  // When the task resolves, Promise.then() logs the result.
  scheduler
    .postTask(() => "Task executing")
    .then((taskResult) => console.log(`${taskResult}`)) // Log result
    .catch((error) => console.error(`Error: ${error}`)); // Log errors
}

For more comprehensive example code see Prioritized Task Scheduling API > Examples.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Scheduler 94 94 101 No 80 No 94 94 No 66 No 17.0
postTask 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/Scheduler