AJAX - when user leaves page - good or bad practice/implementation?

情到浓时终转凉″ 提交于 2019-12-11 02:55:37

问题


I was just reading how to check users leave a page earlier on when suddenly I thought of performing AJAX when user leaves the page to send analytical data back to the server.

Is performing AJAX on the onunload event a good or bad practice/implementation?


回答1:


Performing AJAX calls on unload does not work very well in all situations. Usually, the web browser closes the panel almost immediately and the AJAX call may be lost.

If you want to send analytical data, I suggest to send them in advance of closing the page. For example, send it after 5sec, then 10sec, 20sec, etc. Use unique identifier to find the latest information. The interval is up to you based on how precise measuring you need and not to annoy the user for additional bandwidth :-)




回答2:


To get any reliable form of data you will need to pause the action until the ajax post goes through - the page will usually be terminated before that can happen.

In all reality, there is no way to detect every way a user can get out of a page, onunload, if its not fast enough, will be interrupted.




回答3:


I think you'll end up in race condition

If you spawn another thread, the one who did the AJAX call will be destroyed and the page will unload (because the page was waiting for this thread to finish)

Anyways, unload event are to be avoided at all costs in my opinion! Unless things have changed in the last couple years.. where I told to myself to never do it again :P

Good luck



来源:https://stackoverflow.com/questions/1619946/ajax-when-user-leaves-page-good-or-bad-practice-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!