I refreshed the same page using Chrome browser (macbook) several times, and found that there is a big duration between beforeunload and unload events, randomly from 10ms to 5000ms. This confused me, and I tried to find out why but got no answer. So what exactly happen in the unloading process? Why does this happen?
Here are my measuring codes (with the preserve log option checked, or the log will be cleared when refreshing the page):
window.addEventListener('beforeunload', () => {
console.time('unload duration')
})
window.addEventListener('unload', () => {
console.timeEnd('unload duration')
})