window.addEventListener("unload", (event) => {
// Some long running code
});PerformanceNavigationTiming: unloadEventStart property
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
const unloadEventTime = entry.unloadEventEnd - entry.unloadEventStart;
if (unloadEventTime > 0) {
console.log(
`${entry.name}: unload event handler time: ${unloadEventTime}ms`,
);
}
});
});
observer.observe({ type: "navigation", buffered: true });const entries = performance.getEntriesByType("navigation");
entries.forEach((entry) => {
const unloadEventTime = entry.unloadEventEnd - entry.unloadEventStart;
if (unloadEventTime > 0) {
console.log(`${entry.name}:
load event handler time: ${unloadEventTime}ms`);
}
});