延迟页面关闭Javascript? [英] Delay page close with Javascript?

查看:143
本文介绍了延迟页面关闭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)
}

谢谢!

推荐答案

如果你真的需要(即。准备采用半黑客)延迟关闭页面而不显示确认对话框等,您可以执行以下操作:

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

警告很明显:它不会一直有效,你不能拖延太久。话虽这么说,如果你真的对此感兴趣,你可能会得到超过95%的结果(实际上取决于服务器的响应时间)。

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

这篇关于延迟页面关闭Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆