Delay page close with Javascript?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 17:06:46

问题


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!


回答1:


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).



来源:https://stackoverflow.com/questions/8350215/delay-page-close-with-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!