'Delay page close with Javascript?

Now, I understand that it's bad practice to delay a page close, and that there are better ways to handle that kind of stuff, but just for future reference, is there a way to delay the page closing? Something like

window.onunload = unload();

function unload()
{
setTimeout("self.close()", 1000)
}

Thanks!



Solution 1:[1]

onbeforeunload doesn't work with timeout to protect the browser user from being.

The only way to prevent the page from exiting after the user attempts to leave is by putting synchronous code in the onbeforeunload/onunload handler

But there is something you can do!!!

for(var i = 0; i < 2000; i++){
    console.log(i);
}

You can make this for loop printing to console to delay the unload of the page.

The higher number will take delay high to reload page.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shurvir Mori