Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Note: This feature is available in Web Workers, except for Service Workers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The toJSON() method is a serializer; it returns a JSON representation of the PressureRecord object.
toJSON()None.
A JSON object that is the serialization of the PressureRecord object.
toJSON methodIn this example, calling lastRecord.toJSON() returns a JSON representation of the PressureRecord object.
function callback(records) {
const lastRecord = records[records.length - 1];
console.log(lastRecord.toJSON);
}
try {
const observer = new PressureObserver(callback);
await observer.observe("cpu", {
sampleInterval: 1000, // 1000ms
});
} catch (error) {
// report error setting up the observer
}This would log a JSON object like so:
{
"source": "cpu",
"state": "fair",
"time": 1712052746385.347
}To get a JSON string, you can use JSON.stringify(lastRecord) directly; it will call toJSON() automatically.