Code
Browser Web APIs
Allows web applications to redirect audio output to a permitted Bluetooth headset, speakerphone, or other device.
Provides a way for browsers to better manage audio sessions and inform browsers about what kind of audio is being played.
Provides a method for managing downloads that may take a significant amount of time, running in the background if the user navigates away.
Enables web apps to defer tasks to be run in a service worker once the user has a stable network connection.
Allows scheduling of non-critical work to be performed during idle periods via requestIdleCallback.
Allows installed web apps to set a badge on the app icon to notify users of pending activity such as messages or updates.
Detects linear and two-dimensional barcodes, including QR codes, in images and video streams.
Provides information about the battery charge level of the device and notifications when the level or status changes.
Sends asynchronous, non-blocking requests to a server without expecting a response, ideal for analytics and diagnostics.
Enables simple communication between browsing contexts such as tabs, windows, and iframes sharing the same origin.
Background Tasks API
Provides a cooperative scheduling mechanism via requestIdleCallback() that lets the browser invoke a callback during idle periods — time frames where no urgent tasks are pending. Ideal for non-critical background work that should not block animations or user interactions.
Key Interfaces
requestIdleCallback(callback, options?)— Schedules a callback to run during the next idle period. Returns a numeric handle.cancelIdleCallback(handle)— Cancels a previously registered idle callback.IdleDeadline.timeRemaining()— Returns the estimated remaining idle time in milliseconds for the current frame.IdleDeadline.didTimeout—trueif the callback was invoked because thetimeoutoption expired.
Example — Process a task queue during idle time
Break a large workload into chunks and process items only while the browser is idle, yielding back to the event loop between frames.
const taskQueue = Array.from({ length: 500 }, (_, i) => i);
function processTasks(deadline) {
while (taskQueue.length > 0 && deadline.timeRemaining() > 0) {
const task = taskQueue.shift();
performWork(task); // your non-critical work here
}
if (taskQueue.length > 0) {
// More work left — schedule the next idle callback
requestIdleCallback(processTasks);
}
}
requestIdleCallback(processTasks);Example — Timeout fallback
Use timeout to guarantee the callback runs within a maximum wait time, even if the browser never becomes truly idle. Check didTimeout to decide whether to do a smaller unit of work.
requestIdleCallback(
(deadline) => {
if (deadline.didTimeout) {
// Forced run — do minimal work to avoid jank
processOneTask();
} else {
// Plenty of idle time — process as much as possible
while (deadline.timeRemaining() > 5 && taskQueue.length) {
processOneTask();
}
}
},
{ timeout: 2000 } // run within 2 seconds regardless
);Example — Cancel an idle callback
const handle = requestIdleCallback(() => {
console.log('Running background analytics...');
sendAnalytics();
});
// Cancel before it runs (e.g. user navigated away)
cancelIdleCallback(handle);