Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 409
MatthewSot Avatar asked Dec 02 '11 00:12

MatthewSot


1 Answers

If you really need (ie. ready to resort to semi-hacks) to delay the page closing without showing a confirmation dialog, etc, you can do something like the following:

function delay(ms) {
    var start = +new Date;
    while ((+new Date - start) < ms);
}

// start image loading (I assume you need this for tracking?)
delay(150);

The caveats are obvious: it will not always work and you cannot delay for too long. That being said, if you are really interested in this, you can probably get results of over 95% (really depends on the server response time).

like image 148
deviousdodo Avatar answered Sep 30 '22 09:09

deviousdodo