JavaScript的:等到Ajax请求完成关闭页面 [英] Javascript: wait until ajax request finishes to close page

查看:468
本文介绍了JavaScript的:等到Ajax请求完成关闭页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想浏览器保持页面打开,直到Ajax请求被发送。这就是我想象它看起来像

I would like the browser to keep the page open until the ajax requests are sent. This is what I imagine it would look like

var requestsPending = 0;

window.onbeforeunload = function() {
    showPleaseWaitMessage();
    while(requestsPending > 0);
}

// called before making ajax request, atomic somehow
function ajaxStarted() {
    requestsPending++;
}
// called when ajax finishes, also atomic
function ajaxFinished() {
    requestsPending--;
}

不幸的是,JS没有做多线程。据我了解,回调(ajaxFinished)将永远不会被执行,因为浏览器将尝试等待,直到while循环完成执行它,所以它会永远循环下去。

Unfortunately, JS doesn't do multi-threading. To my understanding, the callback (ajaxFinished) would never be executed because the browser would try to wait until the while loop finishes to execute it, and so the it would loop forever.

什么是正确的方式做到这一点?有没有可能是一种强制JS来评估其待办事项列表,接下来的事情,然后再回到while循环?或者一些语法加盟与一个Ajax调用?我使用DWR我的AJAX。

What's the right way to do this? Is there maybe a way to force JS to evaluate the next thing in its to-do list and then come back to the while loop? Or some syntax to "join" with an ajax call? I'm using DWR for my ajax.

谢谢, -max

推荐答案

修改根据下方的注释,修改后的回答:

Edit Based on your comment below, a revised answer:

如果你想阻止,直到一个的 previously启动的请求完成后,你可以做到这一点是这样的:

If you want to block until a previously-initiated request completes, you can do it like this:

window.onbeforeunload = function(event) {
    var s;
    event = event || window.event;
    if (requestsPending > 0) {
        s = "Your most recent changes are still being saved. " +
            "If you close the window now, they may not be saved.";
        event.returnValue = s;
        return s;
    }
}

浏览器会提示用户,询问他们是否要离开页面或留在这,使他们在控制。如果他们留在页面上,并请求完成而迅速上涨,下一次他们去关闭该页面,它就会让他们关闭它不问。

The browser will then prompt the user to ask whether they want to leave the page or stay on it, putting them in control. If they stay on the page and the request has completed while the prompt was up, the next time they go to close the page, it'll let them close it without asking.

更多关于询问用户是否要取消关闭事件这里和的here

More on asking the user whether to cancel close events here and here.

旧的答案

在理想情况下,如果可能的话,你想的避免的这样做。 : - )

Ideally, if possible, you want to avoid doing this. :-)

如果您不能避免,有可能使一个Ajax请求的同步的,所以它会阻止 onbeforeunload的过程,直到它完成。不知DWR,但我希望它有一个标志,用于控制请求是否是同步还是不。在原料XmlHTT prequest API,这是第三个参数打开

If you can't avoid it, it's possible to make an Ajax request synchronous, so that it blocks the onbeforeunload process until it completes. I don't know DWR, but I expect it has a flag to control whether the request is synchronous or not. In the raw XmlHTTPRequest API, this is the third parameter to open:

req.open('GET', 'http://www.mozilla.org/', false);
                                            ^ false = synchronous

大多数将有一个等价的。例如,在原型,它的异步:假标记中的选项

Most libraries will have an equivalent. For instance, in Prototype, it's the asynchronous: false flag in the options.

但同样,如果你都不可能避免发射了Ajax请求的页面卸载的一部分,我会的。会有的明显的延迟,而请求设置,发送,并完成。好多有服务器使用超时倒闭不管它是什么,你试图关闭与此有关。 (它可以是一个相当短的超时;你可以通过保持会话活着的同步的Ajax请求定期在页面上,而它的开放 —再说,一是一分钟,超时两分钟后。 )

But again, if you can possibly avoid firing off Ajax requests as part of the page unload, I would. There will be a noticeable delay while the request is set up, transmitted, and completed. Much better to have the server use a timeout to close down whatever it is that you're trying to close down with this. (It can be a fairly short timeout; you can keep the session alive by using asynchronous Ajax requests periodically in the page while it's open — say, one a minute, and time out after two minutes.)

这篇关于JavaScript的:等到Ajax请求完成关闭页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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