Javascript:等到ajax请求完成关闭页面[重复]

时间:2022-03-28 03:58:57

This question already has an answer here:

这个问题在这里已有答案:

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

我希望浏览器保持页面打开,直到发送ajax请求。这就是我想象的样子

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

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不做多线程。根据我的理解,回调(ajaxFinished)永远不会被执行,因为浏览器会尝试等待,直到while循环完成执行它,因此它将永远循环。

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.

这样做的正确方法是什么?有没有办法强制JS评估其待办事项列表中的下一个东西,然后回到while循环?或者使用ajax调用“加入”一些语法?我正在使用DWR作为我的ajax。

Thanks, -Max

谢谢,-Max

2 个解决方案

#1


11  

Edit Based on your comment below, a revised answer:

编辑根据您在下面的评论,修改后的答案:

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.

然后,浏览器将提示用户询问他们是想要离开页面还是留在页面上,将其置于控制之下。如果他们留在页面上并且提示已经完成请求已经完成,那么下次他们关闭页面时,它会让他们在不询问的情况下关闭它。

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

更多关于询问用户是否在此处和此处取消关闭事件的更多信息。


Old answer :

老答案:

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

理想情况下,如果可能,您希望避免这样做。 :-)

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:

如果你无法避免它,可以使Ajax请求同步,以便它阻止onbeforeunload进程直到它完成。我不知道DWR,但我希望它有一个标志来控制请求是否同步。在原始XmlHTTPRequest API中,这是要打开的第三个参数:

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.

大多数图书馆都有一个等价物。例如,在Prototype中,它是选项中的异步:false标志。

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

但同样,如果您可以避免在页面卸载过程中触发Ajax请求,我会的。设置,传输和完成请求时会有明显的延迟。更好的办法是让服务器使用超时来关闭你试图关闭的任何东西。 (它可以是一个相当短的超时;您可以通过在页面打开时定期使用异步Ajax请求来保持会话处于活动状态 - 例如,每分钟一次,并在两分钟后超时。)

#2


4  

In short, you cannot (and shouldn't) do this. If a user closes the browser, it's closing...no unload style events are guaranteed to finish, and something doing AJAX with involves latency is more unlikely to finish.

简而言之,你不能(也不应该)这样做。如果用户关闭了浏览器,它就会关闭...没有任何卸载样式事件可以保证完成,并且执行AJAX的事情涉及延迟更不可能完成。

You should look at firing your events at another point, or change the approach altogether, but making an AJAX call in an unload event is going to unreliable, at best.

你应该考虑在另一个点触发事件,或者完全改变方法,但在卸载事件中进行AJAX调用最多也是不可靠的。

As an addendum to the above on the shouldn't part, think about it this way, how many tabs do you usually have open on any given window? I typically have 4-6 chrome windows open with 5-12 tabs each...should my browser window hang open because 1 of those tabs wants to make some AJAX request I don't care about? I wouldn't want it to as a user, so I wouldn't try and do it as a developer. This is just an opinion of course, but food for thought.

作为对上述内容的补充,不应该分开,这样想一下,你通常在任何给定窗口打开多少个标签?我通常打开4-6个镀铬窗口,每个打开5-12个标签...我的浏览器窗口是否应该打开,因为其中一个标签想要发出一些我不关心的AJAX请求?我不希望它作为用户,所以我不会尝试做它作为开发人员。这当然只是一种意见,但值得深思。

#1


11  

Edit Based on your comment below, a revised answer:

编辑根据您在下面的评论,修改后的答案:

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.

然后,浏览器将提示用户询问他们是想要离开页面还是留在页面上,将其置于控制之下。如果他们留在页面上并且提示已经完成请求已经完成,那么下次他们关闭页面时,它会让他们在不询问的情况下关闭它。

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

更多关于询问用户是否在此处和此处取消关闭事件的更多信息。


Old answer :

老答案:

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

理想情况下,如果可能,您希望避免这样做。 :-)

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:

如果你无法避免它,可以使Ajax请求同步,以便它阻止onbeforeunload进程直到它完成。我不知道DWR,但我希望它有一个标志来控制请求是否同步。在原始XmlHTTPRequest API中,这是要打开的第三个参数:

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.

大多数图书馆都有一个等价物。例如,在Prototype中,它是选项中的异步:false标志。

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

但同样,如果您可以避免在页面卸载过程中触发Ajax请求,我会的。设置,传输和完成请求时会有明显的延迟。更好的办法是让服务器使用超时来关闭你试图关闭的任何东西。 (它可以是一个相当短的超时;您可以通过在页面打开时定期使用异步Ajax请求来保持会话处于活动状态 - 例如,每分钟一次,并在两分钟后超时。)

#2


4  

In short, you cannot (and shouldn't) do this. If a user closes the browser, it's closing...no unload style events are guaranteed to finish, and something doing AJAX with involves latency is more unlikely to finish.

简而言之,你不能(也不应该)这样做。如果用户关闭了浏览器,它就会关闭...没有任何卸载样式事件可以保证完成,并且执行AJAX的事情涉及延迟更不可能完成。

You should look at firing your events at another point, or change the approach altogether, but making an AJAX call in an unload event is going to unreliable, at best.

你应该考虑在另一个点触发事件,或者完全改变方法,但在卸载事件中进行AJAX调用最多也是不可靠的。

As an addendum to the above on the shouldn't part, think about it this way, how many tabs do you usually have open on any given window? I typically have 4-6 chrome windows open with 5-12 tabs each...should my browser window hang open because 1 of those tabs wants to make some AJAX request I don't care about? I wouldn't want it to as a user, so I wouldn't try and do it as a developer. This is just an opinion of course, but food for thought.

作为对上述内容的补充,不应该分开,这样想一下,你通常在任何给定窗口打开多少个标签?我通常打开4-6个镀铬窗口,每个打开5-12个标签...我的浏览器窗口是否应该打开,因为其中一个标签想要发出一些我不关心的AJAX请求?我不希望它作为用户,所以我不会尝试做它作为开发人员。这当然只是一种意见,但值得深思。