Skip to content

Instantly share code, notes, and snippets.

@weizman
Last active January 30, 2019 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weizman/8f5434d884648a704e831c20bda93282 to your computer and use it in GitHub Desktop.
Save weizman/8f5434d884648a704e831c20bda93282 to your computer and use it in GitHub Desktop.
demonstration of what can be done inside an `unload`/`beforeunload` listener
window.addEventListener('unload', (e) => {
function sleep(delay) {
const start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
// unloading won't finish until 10 full seconds pass!
sleep(10000);
setTimeout(() => {
// this part would be ignored since unloading will already
// finish by now, thus it won't wait for this async call
sleep(10000);
}, 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment