可以卸载事件用于可靠火Ajax请求? [英] Can the unload Event be Used to Reliably fire ajax Request?

查看:175
本文介绍了可以卸载事件用于可靠火Ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来监测用户编辑会话,并使用我检讨会中有我的解决方案之一卸载事件来发送一个Ajax请求通知服务器编辑会话的结束。 (参见:<一href="http://stackoverflow.com/questions/3530165/monitoring-user-sessions-to-$p$pvent-editing-conflict">http://stackoverflow.com/questions/3530165/monitoring-user-sessions-to-$p$pvent-editing-conflict)

I need a method to monitor user edit sessions, and one of the solutions I'm reviewing will have me using an unload event to send an ajax request to inform the server of the end of the edit session. (See: http://stackoverflow.com/questions/3530165/monitoring-user-sessions-to-prevent-editing-conflict)

我的(相当有限)读卸载事件表明,codeS连接到该处理器具有快速运行,因此通常用于清除反对prevent内存泄漏。

My (rather limited) reading on the unload event indicate that the codes attached to this handler has to run quickly, and as such is usually used for clearing objects to prevent memory leaks.

我的问题是,这项工作确实够这个目的?

My question is, can this work reliably enough for this purpose?

PS。我知道关于异步:假选项。

PS. I know about the async: false option.

推荐答案

这方法是相当可靠的,如果你的服务器的速度足以应对。东西要真正注意虽然。如果你关闭浏览器和发送AJAX请求卸载事件,有一个非常好的机会,响应是不会来从​​服务器返回的时间前的窗口对象被销毁。会发生什么事在此情况下(至少在IE浏览器)是,它会孤立您的连接对象非正常终止,直到连接超时被击中。如果您的服务器没有连接保持开启,关闭2个窗口后(同时还具有其它窗口中打开),你会耗尽打开的连接到服务器(IE6-7,为IE8 - 6视窗)你将无法打开你的网站,直到你的连接超时被击中。

This method is fairly reliable, if your server is fast enough to respond. Something to really watch out for though. If you close the browser and send AJAX request on unload event, there's a very good chance that the response isn't going to come back from the server in time before the window object is destroyed. What happens in this case (at least with IE) is that it will orphan your connection object and not terminate it correctly until the connection timeout is hit. If your server doesn't have connection keep-alive turned on, after you close 2 windows (while still having another window open), you will run out of open connections to the server (for IE6-7, for IE8 - 6 windows) and you will not be able to open your website until your connection timeout is hit.

我遇到了这样的情况之前,我开口,将派遣上卸载一个AJAX请求一个弹出窗口,它是非常可靠的,但它困扰着上面的发行说明,并花了很长一段时间,我跟踪下来,并了解发生了什么事情。在那之后,我做什么,是我确信,打开的窗口将具有相同的code调用服务器,并在每个卸载检查的揭幕战,跑了code那里,如果它是present。

I ran into a situation like that before were I was opening a popup window that was sending an AJAX request on unload, it was very reliable, but it was plagued by the issued described above, and it took really long time for me to track it down and understand what's going on. After that, what I did, is I made sure that opening window would have the same code to call server, and on every unload checked for the opener and ran the code there if it was present.

看来,如果您关闭最后浏览器窗口,IE会破坏连接正常,但如果其他窗口打开时,它不会。

It seems that if you close the very last browser window, IE will destroy connection properly, but if one other window is open, it will not.

P.S。而刚刚以上面的回答发表评论,AJAX是不是真的异步。至少JS实现的事实并非如此。当你发送一个请求,你JS code仍然要等待来自服务器的响应。它不会阻止你的code执行,但由于服务器可能需要一段时间来响应(或足够长的Windows来终止IE浏览器窗口对象),你可能会,可能会遇到上述问题。

P.S. And just to comment on the answer above, AJAX is not really async. At least JS implementation of it isn't. After you send a request, you JS code is still going to be waiting for response from the server. It's not going to block your code execution, but since the server might take a while to response (or long enough for Windows to terminate IE window object) you might and probably will run into the problem described above.

这篇关于可以卸载事件用于可靠火Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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