关闭浏览器窗口后,是否有可能发送AJAX? [英] Is there any possibility how to send an AJAX after close the browser window?

查看:113
本文介绍了关闭浏览器窗口后,是否有可能发送AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关闭浏览器窗口后,是否有可能发送AJAX?

Is there any possibility how to send AJAX after close the browser window?

我有一个使用JavaScript(jQuery)进行移动的浏览器游戏,如果每次移动后都发送Ajax,则服务器将很困难.所以我想在用户关闭窗口(或书签)时发送一个AJAX. 它必须在所有现代浏览器中都可以正常工作.

I have a browser game with movement in JavaScript (jQuery) and if I send Ajax after each movement it would be difficult for the server. So I want to send one AJAX when user close the window (or bookmark). It must be functional in all modern browsers.

谢谢您的回答

推荐答案

我建议您使用某种计时器来更新服务器,以使服务器在了解客户端所处的状态时永远不会过分落后(也许每60分钟一次)客户端处于活动状态时的秒数),则在客户端处于非活动状态时暂停服务器更新.

I'd suggest you update the server on some sort of timer so the server never gets too far behind in knowing what state the client is in (perhaps every 60 seconds when the client is active), pausing server updates when the client is not active.

然后,在用户界面中,为关闭"或停止"添加一些明显的用户界面元素,以鼓励用户以这种方式关闭,然后在按下其中任何一个按钮时更新服务器.

Then, in your user interface, put some obvious user interface elements for Close or Stop that encourages the user to shut-down that way and then update the server when either of those buttons are hit.

然后,您还可以挂接页面的unload事件,然后发送最后一个ajax调用.但是,并不是在所有情况下都不会调用此方法,也不是所有浏览器都支持此方法,因此,除了前两种技术之外,还会执行此操作.

Then, you can also hook the unload event for the page and send one last ajax call then. But, this is not called in every case or supported in all browsers so this would be done in addition to the two previous techniques.

这篇关于关闭浏览器窗口后,是否有可能发送AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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