如何发送AJAX请求,当用户离开(关闭)的页面? [英] How to send AJAX request when user leaves (closes) the page?

查看:171
本文介绍了如何发送AJAX请求,当用户离开(关闭)的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的现状

你好, 我在做一个数据库驱动的游戏,其中有一个问题,用户获得改变的问题的权利(即写了一个新问题),如果他们找到正确的答案。

但是,这里有一个重要的问题,我想这就是所谓的竞争状态。问题是这样的:

  • 在用户1找到答案
  • 在用户1被提示输入一个新的问题
  • 在用户1提交新的问题,用户2找到了新的答案太
  • 现在,这只是运气,是谁提交新问题。如果用户2用户1日后提交的问题,用户2提问覆盖用户1的问题。

现在,我已经通过使用某种信号量,通过保持其时间戳修正了这个问题。总结它的工作原理是这样的:

  • 在用户1找到答案
  • 在信号灯被保存到数据库中,比如 ['user_ip'='XX','时间'='YY','锁定'='真']
  • 信号灯t秒后过期
  • 新的问题将只接受信号已过期。

所以,当一个新的问题被提交,申请接受它只有当信号量的不可以锁定,并且当前时间是 T (15)秒,比信号灯的日期。

但是这有一个问题太多,用户2提示

  

你已经找到了答案,但有人发现它之前,你,你可以   改变的问题,如果它们不在15提交一个新的问题   秒。

因此​​,新的赢家必须等待15秒,也许是不必要的。

因此​​,

我想要做

我想提示的胜利者的一种形式,用秒表(时间计数器)。当它达到零,它释放了信号,使新的赢家可以提交自己的问题。

但是,这种解决方案都会有这样的问题:

如果获胜者关闭之前的时间计数器达到零的页面,那么AJAX请求永远不会完成,信号量将被无限期锁定。

所以,我必须确保我称之为 userLeave(); 功能,如果用户关闭了页面

谷歌暗示 onbeforeunload的 onunload 但人们也抱怨那些不工作的功能。那么,有没有送这个AJAX调用,如果用户(胜利者)离开该页面的100%-sure方式?

解决方案
  

有没有送这个AJAX调用,如果用户(胜利者)离开该页面的100%-sure方式?

您可以 永远靠客户端的做任何事情100%。想想网络连接损耗,浏览器崩溃,没有的特性(从HTML5不兼容的浏览器禁用了JavaScript),脚本错误,错误配置的代理那些叫(或不)以意想不到的方式或黑客做同样的目的,你的API ...

您可以期待一个客户端的唯一的事情是,他什么都不做 - 这就是所谓的超时

如果你希望你的信号过期在一定的时间,你必须创建计时器服务器端。如果你愿意,你可以另外中止定时器和过期immediatley当客户端信号,他是不会提交答案(由两者的意思)。如果你想尝试赶上关闭页面,则可以使用同步(A)JAX从卸载事件。

顺便说一句:虽然我不知道你的游戏,它会更容易,也许你根本就同时接受新的问题更舒适: - )

The Situation

Hi, I'm making a database-driven game, in which there is a question, and a user gains the right to change the question (i.e. write a new question) if they find the correct answer.

But this has an important issue, I think it's called a race condition. The issue is this:

  • User 1 finds the answer
  • User 1 is prompted to enter a new question
  • Before User 1 submits the new question, User 2 finds the new answer too
  • Now it's only luck, who is submitting the new question. If User 2 submits the question after User 1, User 2's question overwrites User 1's question.

Right now, I've fixed this issue by using some kind of semaphore, by keeping its timestamp. To summarize it works like this:

  • User 1 finds the answer
  • Semaphore is saved into database like ['user_ip' = 'x.x' , 'time'='y.y' , 'locked'='true']
  • Semaphore expires after t seconds
  • New question will be accepted only if semaphore is expired.

so when a new question is submitted, the application accepts it only if the semaphore is not locked, and the current time is larger by t (15) seconds than the semaphore's date.

But this has a problem too, User 2 is prompted

"You have found the answer, but somebody found it before you. You can change the question if they don't submit a new question in 15 seconds."

So the new winner has to wait 15 seconds, maybe unnecessarily.

Therefore,

What I Want to Do

I want to prompt the "winner" a form, with a stopwatch (time counter). When it reaches zero, it unlocks the semaphore so that the new winner can submit their question.

But this solution will have this problem:

If the winner closes the page before time counter reaches to zero, then the AJAX request will never be done and the semaphore will be locked indefinitely.

So I have to make sure I have called the userLeave(); function if the user closes the page.

Google suggests onbeforeunload and onunload but people also complain about those functions not working. So is there a 100%-sure way to send this AJAX call if a user (a winner) leaves the page ?

解决方案

Is there a 100%-sure way to send this AJAX call if a user (a winner) leaves the page?

You can never rely on the client doing anything 100%. Think of network connectivity losses, browser crashes, not available features (from HTML5-incompatible browsers to disabled javascript), scripting errors, misconfigured agents that do call (or not) your API in unexpected ways or hackers doing the same on purpose…

The only thing you can expect of a client is that he does nothing - which is called a timeout.

If you want your semaphore to expire at a certain time, you have to create the timer serverside. If you want, you can additionally abort the timer and expire immediatley when a client signals that he's not going to submit an answer (by whichever means). If you want to try to catch closing the page, you can use synchronous (A)JAX from an unload event.

Btw: While I don't know your game, it would be easier and probably more comfortable if you simply would accept both new questions :-)

这篇关于如何发送AJAX请求,当用户离开(关闭)的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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